Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Java
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.
Posted
Updated 25-Aug-22 9:07am
v2

Quote:
Obviously nothing is being read in.

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.
 
Share this answer
 
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.
 
Share this answer
 
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:
Java
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:

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


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.

Java
try { 
  this.value_1 = System.in.read() - '0';
}catch( IOException e) {
  System.err.println("Could not read from the input stream: " + e.getMessage());
}
 
Share this answer
 
Disclaimer: I am relatively new to programming so my solution is very simple.

My main focus was getting the scanner input/output to work properly as requested.

I am not sure what the purpose of the try/catch was so I omitted it.

I set the int user_choice variable so that the switch would run. Feel free to change as needed.
C#
int user_choice = 1;

		switch (user_choice) {

        case 1:  
         System.out.println("Please enter a number..."); 			  			 

         Scanner sc1 = new Scanner(System.in);            	 

         int value_1 = Integer.parseInt(sc1.nextLine());
		  
         System.out.println("You entered:" + value_1);

		}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900