Java扫描器输入困境。自动输入而不允许用户键入

Java扫描器输入困境。自动输入而不允许用户键入,java,input,java.util.scanner,Java,Input,Java.util.scanner,非常感谢您的回复,我可能会坚持只添加额外的input.nextLine()语句来捕获任何“剩余内容” 因此,在这段代码中,我输入了2,一旦它进入if语句,它就会跳过“sCreateLogin=input.nextLine();”并进入下一个输入。可能是因为扫描仪中有东西挥之不去,但我无法弄清楚它为什么会这样做,以及如何准确地修复它 如果我输入了input.next(),它会停止,但这还不够好,因为如果你不小心添加了一个空格,它也会跳过下一个输入。我知道我可以解析它等等,但我仍然对此感到困惑 Sc

非常感谢您的回复,我可能会坚持只添加额外的input.nextLine()语句来捕获任何“剩余内容”

因此,在这段代码中,我输入了2,一旦它进入if语句,它就会跳过“sCreateLogin=input.nextLine();”并进入下一个输入。可能是因为扫描仪中有东西挥之不去,但我无法弄清楚它为什么会这样做,以及如何准确地修复它

如果我输入了input.next(),它会停止,但这还不够好,因为如果你不小心添加了一个空格,它也会跳过下一个输入。我知道我可以解析它等等,但我仍然对此感到困惑

Scanner input = new Scanner(System.in);
System.out.println("(1) Login");
System.out.println("(2) Create Account");
int iAccountOption = input.nextInt();
if(iAccountOption==2)
{
System.out.println("Input desired login: ");
String sCreateLogin = input.nextLine();
System.out.println("Input desired password: ");
String sCreatePassword = input.nextLine();
}

问题可能是未处理的行尾令牌。要解决此问题,请在input.nextInt()之后;添加一个额外的input.nextLine()以吞咽行尾标记:

int iAccountOption = input.nextInt();
input.nextLine();
if (iAccountOption == 2) {
   .....

尝试为字符串使用其他扫描程序对象。

它正在跳过sCreateLogin,因为Scanner.nextLine()已具有值“\r\n”。 因此,我将所有扫描仪更改为nextLine()。结果很好,但也许这不是最好的主意

package com.stackoverflow.main;

import java.util.Scanner;

public class SO4524279 {
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("(1) Login");
    System.out.println("(2) Create Account");
    int iAccountOption = new Integer(scanner.nextLine());
    String sCreateLogin = "";
    String sCreatePassword = "";
    if (iAccountOption == 2) {
        System.out.println("Input desired login: ");
        sCreateLogin = scanner.nextLine();
        System.out.println("Input desired password: ");
        sCreatePassword = scanner.nextLine();
    }
    System.out.println("Login: " + sCreateLogin + "Pass: " + sCreatePassword);
}

}

记住在新整数上使用try catch(scanner.nextLine())

我建议您使用以下两种方法之一: 1.使用BufferedReader类 1a.使用BufferedReader类并用InputStreamReader类包装它

BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
//string str = br.readLine();     //for string input
int i = Integer.parseInt(br.readLine());     // for Integer Input
1b.既然readLine方法抛出IOException,那么您需要捕获它。因此,整个代码将如下所示

try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
//string str = br.readLine();     //for string input
int i = Integer.parseInt(br.readLine());     // for Integer Input
}catch(IOException ioe){
ioe.PrintStackTrace();
}
2.如果您使用的是Java SE6或更高版本,那么您可以使用Console类

Console cons = System.console();
String str = cons.readLine("Enter name :");
System.out.print("your name :"+str);

这就是我有时所做的。仍然想知道为什么会发生这种情况:0谢谢。正如我前面提到的,它之所以这样做是因为您有未被“吞没”或处理的线尾令牌,并且您对nextLine的下一次调用在用户有机会输入任何新数据之前吞没了这些令牌。如果您获取一个非nextLine令牌,并且它总是一个新行,那么您将需要额外调用nextLine方法。我认为已经提到的另一个解决方案是使用nextLine获取整行,然后使用另一个Scanner对象解析获得的字符串。
Console cons = System.console();
String str = cons.readLine("Enter name :");
System.out.print("your name :"+str);