0

How can i take input from scanner ...like i code and "what is your age prints on the screen " and i will put if else condition ?

https://code.sololearn.com/cTu66YtJMCxK/?ref=app

26th May 2020, 10:36 AM
Harsh Vyas
Harsh Vyas - avatar
7 odpowiedzi
+ 1
Take care what you are assigning: final Scanner scanner =new Scanner(System.in); final int age = scanner.nextInt(); // now work with age // afterwards scanner.close();
26th May 2020, 4:31 PM
Sandra Meyer
Sandra Meyer - avatar
+ 3
See scanner is used for taking inputs so it should go like: Scanner s= new Scanner(System.in); Now you use scanner to take input and store it in variable: int age=s.nextInt(); Now that you have the input given by user in variable age, you can print and compare variable age.
26th May 2020, 10:58 AM
Chetali Shah
Chetali Shah - avatar
+ 3
In your code you have age as a Scanner object and are trying to use it like an integer. Your age.nextInt() gets an integer from the input stream and just returns it to System.out.println() outputting it and never saving its value. int age =new Scanner(System.in).nextInt(); System.out.println(age); if (age < 16) { System.out.println("Too Young"); } else { System.out.println("Welcome!"); }
26th May 2020, 10:58 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
It supposed to be Scanner sc = new Scanner(System.in); //You should ask user to insert their age System.out.println("what is your age prints on the screen: "); int age = sc.nextInt(); //Next statement which is your else if condition
27th May 2020, 5:52 PM
Afa_rraf
Afa_rraf - avatar
0
It should be: Scanner s = new Scanner(System.in); and not System s = new Scanner (System.in);
26th May 2020, 10:37 AM
Chetali Shah
Chetali Shah - avatar
26th May 2020, 10:45 AM
Harsh Vyas
Harsh Vyas - avatar
0
Now it is also giving error
26th May 2020, 10:45 AM
Harsh Vyas
Harsh Vyas - avatar