1

編集: 入力は 25565、127.0.0.1、25565、testpassword、停止

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) トークンを処理しないことに注意してください。 () 別の行を取得すると、最初の 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 に答える