java.util.InputMismatchException

标签 java sockets client-server

编辑:输入为 25565、127.0.0.1、25565、测试密码、停止

我编写了一些简单的代码来通过 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。

最佳答案

请注意,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);

关于java.util.InputMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13656113/

相关文章:

c - 在 C 中通过 TCP/IP 发送和接收字节数组

java - 为什么关闭客户端后服务器停止运行?

ios - iOS 客户端/服务器应用程序

java - 显示 Hibernate SQL 的 Logback 记录器必须大写

java - Tesseract - 错误 net.sourceforge.tess4j.Tesseract - 空

java - Arrays.asList().contains() 给出错误的结果

c - struct sockadr_in 不应该同时适用于 IPv4 和 IPv6 吗?

C++ 在 map 中放置()时防止析构函数调用

java - 客户端-服务器基础设施中的 Applet

java - Spring @Transactional 属性是否适用于私有(private)方法?