等待用户输入(扫描仪类) [英] Waiting for user input (scanner class)

查看:64
本文介绍了等待用户输入(扫描仪类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到的挑战是,在运行时,用户被要求输入数字0-9,然后在他们有机会输入数字之前,它跳转到说你输入了:null。显然没有被读入。以前使用过BufferedReader类但后来发现它与扫描器类相比提供的更少。有效的我想要发生的是应用程序等待用户输入数字,然后显示用户输入,以便用户可以在继续之前验证是否有选择。



The challenge I'm having here is that at run time the user is asked to enter there number 0-9 then before they get a chance to enter there number it jumps to say "You entered: null". Obviously nothing is being read in. Previously used the BufferedReader class but later found out that it offers less in comparison to the scanner class. Effective what I want to happen is for the application to wait for the user to enter there number then display the user input so the user can verify there choice before proceeding.

switch (user_choice) {

            case 1:  
            
           //Ask the user for there input...  <<<<
             System.out.println("Please enter a number..."); 			  			 
   			 
             try{
            	 
            	 Scanner sc1 = new Scanner(System.in);            	 
            	
             
             this.value_1 = Integer.parseInt(sc1.nextLine());
             
             
             
             //sc1.close();
             
             
             }catch (Exception e) {
				// TODO: handle exception
            	 
            	 
			}
             
             
   		  
             System.out.println("You entered:" + this.value_1);





我的尝试:





What I have tried:

1. Changed  from using BufferedReader class to scanner class as it many have a method that may assist me in achieving my goal.

推荐答案

引用:

显然没有读入任何内容。

Obviously nothing is being read in.



显然,你有一个try-catch结构可以阻止你应用程序告诉你出了什么问题。

首先要删除try-catch以获得有意义的错误消息。


Obviously, you have a try-catch structure which prevent your app from telling you what go wrong.
First thing is to remove the try-catch to get a meaningful error message.


参见扫描程序(Java Platform SE 7) [ ^ ]。如果要忽略所有异常,使用try / catch构造没有意义。
See Scanner (Java Platform SE 7 )[^]. And there is no point in using a try/catch construct if you are going to ignore all exceptions.


此时,不需要使用包装类来获取一个 - 您输入的数字编号。



你可以简单地使用:

At this point, there is no need to use the wrapper class to get a one-digit-number you've entered.

You can use simply:
int value = sc1.nextLine().charAt(0) - '0';





请记住,当没有给出输入时,此代码将崩溃。您应该更喜欢异常处理:





Keep at mind, that this code will crash when there was no input given. You should prefer exception handling:

try { 
  this.value_1 = sc1.nextLine().charAt(0) - '0';
}catch( NullPointerException | StringIndexOutOfBoundsException e) {
  System.err.println("Invalid input.");
}





如果您只想读取这个单字节信息,可以避免使用扫描仪类并直接从InputStream获取输入。



System.in.read()(来自InputStream )方法从stdin读取单个字节,请记住,这可能会抛出 IOException





If you just want to read this single one byte information, you can avoid the use of the Scanner class and get the input from the InputStream directly.

The System.in.read() ( from InputStream ) method reads a single byte from stdin, keep in mind, that this may throw a IOException if there are some errors.

try { 
  this.value_1 = System.in.read() - '0';
}catch( IOException e) {
  System.err.println("Could not read from the input stream: " + e.getMessage());
}


这篇关于等待用户输入(扫描仪类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆