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

3
非常感谢您的回复,我可能会坚持只添加额外的input.nextLine()语句来捕获任何“残留”。在这段代码中,我输入了2,一旦它进入if语句,它跳过了“sCreateLogin = input.nextLine();”,并继续进行下一个输入。很可能是因为还有其他东西留在了Scanner中,但我无法弄清楚它为什么这样做以及如何修复它。如果我使用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();
}
5个回答

2
问题可能是未处理的行尾标记。为了解决这个问题,在输入语句input.nextInt();后添加一个额外的input.nextLine()来吞噬行尾标记:
int iAccountOption = input.nextInt();
input.nextLine();
if (iAccountOption == 2) {
   .....

这是我有时候在做的事情。虽然还不明白为什么会发生这种情况,但还是感谢。 - anon
正如我之前提到的,这是因为您有未被“吞咽”或处理的行尾标记,而您下一次调用nextLine会在用户有机会输入任何新数据之前吞咽这些标记。如果您获取一个非nextLine标记,并且它总是一个新行,则需要额外调用nextLine方法。另一个解决方案是使用nextLine获取整行,然后使用另一个Scanner对象解析所获得的字符串,我认为这个解决方案已经被提到过了。 - Hovercraft Full Of Eels

1
我建议您使用以下两种方式之一: 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

它跳过了sCreateLogin,因为scanner.nextLine()已经有了值"\r \n"。所以我把所有的scanner都改成了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);
}

}

记得在 new Integer(scanner.nextLine()) 上使用 try catch


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

0

尝试为字符串使用不同的Scanner对象。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接