来自扫描仪的空白输入 - java

发布于 2024-11-01 19:25:53 字数 578 浏览 5 评论 0原文

我想做的是,如果用户单击回车键,程序应该抛出 BadUserInputException。 我的问题是,每当我按下回车键时,它只会将我置于控制台的另一行中,基本上什么也不做。

Scanner input = new Scanner(System.in);
    System.out.println("Enter Student ID:");

    String sID = null;
    if (input.hasNextInt()==false){
        System.out.println("Please re-check the student number inputted. Student number can only be digits.");
        throw new BadUserInputException("Student number can not contain non-digits.");
    }else if (input.next()==""){
        throw new BadUserInputException("Student number can not be empty");
    }

What I'm trying to do is if the user clicks on the enter key the program should throw a BadUserInputException.
My problem is whenever I press the enter key it just puts me in another line in the console essentially doing nothing.

Scanner input = new Scanner(System.in);
    System.out.println("Enter Student ID:");

    String sID = null;
    if (input.hasNextInt()==false){
        System.out.println("Please re-check the student number inputted. Student number can only be digits.");
        throw new BadUserInputException("Student number can not contain non-digits.");
    }else if (input.next()==""){
        throw new BadUserInputException("Student number can not be empty");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落日海湾 2024-11-08 19:25:53

我遇到了这个确切的问题,并且我相信我找到了解决方案。

关键是接受 String 类型的输入,并使用 .isEmpty() 方法检查用户是否输入了任何内容。

如果您的字符串被命名为“cheese”并且您的扫描器被命名为“in”,则如下所示:

cheese = ""; // empty 'cheese' of prior input
cheese = in.nextLine(); //read in a line of input

//if user didn't enter anything (or just spacebar and return)
if(cheese.isEmpty()) {
System.out.println("Nothing was entered. Please try again");
} 
//user entered something
else {
[enter code here checking validity of input]
}

我尝试对整数输入实现此检查,发现最好接受字符串类型输入并使用包装类将其转换为 int 类型。如果您有

int eger = 0; //initialize to zero

,那么您可以将此代码放在上面的 else 语句中:

else{
eger = Integer.valueOf(cheese);
}

我知道这个问题是在 2 年前提出的,但我发布此问题是为了帮助像我这样正在寻找解决方案的其他人回答。 :)

I am encountering this exact problem, and I believe I figured out a solution.

The key is to accept input as type String and use the .isEmpty() method to check whether the user entered anything or not.

If your String is named "cheese" and your scanner is named "in", here's what this looks like:

cheese = ""; // empty 'cheese' of prior input
cheese = in.nextLine(); //read in a line of input

//if user didn't enter anything (or just spacebar and return)
if(cheese.isEmpty()) {
System.out.println("Nothing was entered. Please try again");
} 
//user entered something
else {
[enter code here checking validity of input]
}

I tried implementing this check for integer input and discovered it's better to accept String type input and convert it to int type with a wrapper class. If you have

int eger = 0; //initialize to zero

then you would put this code in the else statement above:

else{
eger = Integer.valueOf(cheese);
}

I am aware that this question was asked 2 years ago, but I posted this in hopes of helping others like myself who were looking for an answer. :)

演出会有结束 2024-11-08 19:25:53

扫描器会查找空格和换行符之间的标记 - 但没有任何标记。我不倾向于使用 Scanner 从标准输入读取 - 我使用老式的 BufferedReader 方法,如下所示:

BufferedReader buf = new BufferedReader (new InputStreamReader (System.in));

然后我可以说

String line = buf.readLine ();
if (line.equals ("")) blah();

可能有一个更简单的解决方案然而。

The scanner looks for tokens between whitespaces and newlines - but there aren't any. I don't tend to use Scanner for reading from standard input - I use the old-fashioned BufferedReader method like this:

BufferedReader buf = new BufferedReader (new InputStreamReader (System.in));

and then I can say

String line = buf.readLine ();
if (line.equals ("")) blah();

There may be an easier solution however.

情泪▽动烟 2024-11-08 19:25:53

只需在扫描字符串时使用 nextLine() 方法而不是 next() 即可,最后使用 isEmpty() 方法检查字符串。这对我有用..

Just use nextLine() method instead of next() while scanning the String and finally check the string using isEmpty() method. This Worked for me..

初吻给了烟 2024-11-08 19:25:53

您需要使用 .equals 方法而不是 == 来比较字符串。

You need to compare strings with the .equals method, not ==.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文