简体   繁体   中英

java.util.InputMismatchException

EDIT: Input was 25565, 127.0.0.1, 25565, testpassword, stop

I've wrote some simple code to send a command over RCON to a server, I'm getting this error:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at mainclass.main(mainclass.java:21)

Here is my code:

import net.sourceforge.rconed.*;
import net.sourceforge.rconed.exception.BadRcon;
import net.sourceforge.rconed.exception.ResponseEmpty;

import java.net.SocketTimeoutException;
import java.util.Scanner;
class mainclass {
    public static void main(String args[]) throws SocketTimeoutException, BadRcon, ResponseEmpty{
        Scanner input = new Scanner(System.in);

        String ipStr, password, command;
        int localPort, port;

        System.out.println("Enter local Query port: ");
        localPort = input.nextInt();

        System.out.println("Enter game IP: ");
        ipStr = input.nextLine();

        System.out.println("Enter game port: ");
        port = input.nextInt();

        System.out.println("Enter password: ");
        password = input.nextLine();

        System.out.println("Enter command: ");
        command = input.nextLine();

        Rcon.send(localPort, ipStr, port, password, command);
    }
}

NOTE: the Rcon.send function REQUIRES a int, string, int, string and string respectively.

Note that Scanner#nextInt(), Scanner#nextDouble() and similar methods do not handle the end-of-line (EOL) token, and so you if you use one of these methods and want to follow this with a call to nextLine() get another line, you'll have to handle the first EOL yourself. Try changing

    Scanner input = new Scanner(System.in);

    String ipStr, password, command;
    int localPort, port;

    System.out.println("Enter local Query port: ");
    localPort = input.nextInt();

    System.out.println("Enter game IP: ");
    ipStr = input.nextLine();

    System.out.println("Enter game port: ");
    port = input.nextInt();

    System.out.println("Enter password: ");
    password = input.nextLine();

    System.out.println("Enter command: ");
    command = input.nextLine();

    Rcon.send(localPort, ipStr, port, password, command);

to:

    Scanner input = new Scanner(System.in);

    String ipStr, password, command;
    int localPort, port;

    System.out.println("Enter local Query port: ");
    localPort = input.nextInt();
    input.nextLine();  // ***** added! *****

    System.out.println("Enter game IP: ");
    ipStr = input.nextLine();

    System.out.println("Enter game port: ");
    port = input.nextInt();
    input.nextLine();  // ***** added! *****

    System.out.println("Enter password: ");
    password = input.nextLine();

    System.out.println("Enter command: ");
    command = input.nextLine();

    Rcon.send(localPort, ipStr, port, password, command);

You could try this. Notice I used input.nextLine() for all scenarios.

    System.out.println("Enter local Query port: ");
    localPort = Integer.parseInt(input.nextLine());

    System.out.println("Enter game IP: ");
    ipStr = input.nextLine();

    System.out.println("Enter game port: ");
    port = Integer.parseInt(input.nextLine());

    System.out.println("Enter password: ");
    password = input.nextLine();

    System.out.println("Enter command: ");
    command = input.nextLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM