1

编辑:输入为 25565、127.0.0.1、25565、testpassword、stop

我写了一些简单的代码来通过 RCON 向服务器发送命令,我收到了这个错误:

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)

这是我的代码:

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);
    }
}

注意:Rcon.send 函数分别需要一个 int、string、int、string 和 string。

4

2 回答 2

2

请注意,Scanner#nextInt()、Scanner#nextDouble() 和类似方法不处理行尾 (EOL) 令牌,因此如果您使用这些方法之一并希望通过调用 nextLine () 换行,您必须自己处理第一个 EOL。尝试改变

    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);

至:

    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);
于 2012-12-01T03:46:26.593 回答
0

你可以试试这个。请注意,我在所有场景中都使用了 input.nextLine()。

    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();
于 2012-12-01T03:48:36.003 回答