首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java.util.InputMismatchException

java.util.InputMismatchException
EN

Stack Overflow用户
提问于 2012-12-01 11:38:04
回答 2查看 739关注 0票数 1

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

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

代码语言:javascript
复制
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)

下面是我的代码:

代码语言:javascript
复制
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。

EN

回答 2

Stack Overflow用户

发布于 2012-12-01 11:46:27

注意,Scanner#nextInt()、Scanner#nextDouble()和类似的方法不处理行尾( EOL )令牌,所以如果您使用这些方法之一,并且想要在此之后调用nextLine()获得另一行,则必须自己处理第一个EOL。试着改变

代码语言:javascript
复制
    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);

至:

代码语言:javascript
复制
    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);
票数 2
EN

Stack Overflow用户

发布于 2012-12-01 11:48:36

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

代码语言:javascript
复制
    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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13656113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档