2012-12-01 27 views
1

編輯:輸入爲25565,127.0.0.1,25565,testpassword,停止java.util.InputMismatchException

我已經寫了一些簡單的代碼,在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功能分別需要一個整型,字符串,整數,字符串,字符串。

+0

歡迎來到SO。我會給你+1,除非你沒有顯示提供的確切輸入。我相信@HovercraftFullOfEels的答案可以幫助你解決問題。如果你添加輸入,我會給你+1。 –

回答

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

+1 OP沒有顯示輸入,但這很可能是問題。 –

+0

太棒了,那就是問題所在! –

+0

@DrakeLuce:太棒了 - 很高興它有幫助! –

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(); 
相關問題