+ 1

Can you please show me what are the mistakes in this code? (In the description)

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner name = new Scanner(System.in); Scanner age = new Scanner(System.in); if(age< 16){ System.out.println (name.nextLine()); }else{ Scanner add = new Scanner(System.in); System.out.println (age.nextInt()); } } }

14th Apr 2018, 2:32 PM
Abinash Sarma
Abinash Sarma - avatar
5 Réponses
+ 3
You're not taking any input and storing it. Try this one class Test { public static void main (String[] args) throws Exception { Scanner name = new Scanner(System.in); Scanner age = new Scanner(System.in); String NAME=name.nextLine(); int AGE=age.nextInt(); if(AGE<=16){ System.out.println("Name:"+NAME); }else{ System.out.println("Age:"+AGE); } name.close(); age.close(); } }
15th Apr 2018, 12:36 AM
Ashish Kumar
Ashish Kumar - avatar
+ 1
Without taking the value in age.. how you compare first.. at the time you declared age.. age only hold the reference id of object of scanner class.. you first provide a value.. before compair
14th Apr 2018, 2:54 PM
Arun Tomar
Arun Tomar - avatar
+ 1
You Didn't Entered The Value Of Age.You Should Do that and Run The Code It will Work.
14th Apr 2018, 3:10 PM
Rahil Mavani
Rahil Mavani - avatar
0
bro I'm taking about.. if statement bro..
14th Apr 2018, 3:06 PM
Arun Tomar
Arun Tomar - avatar
- 1
Hey, Arun Tomar I didn't understand. Why not the value of age inserted in the scanner class is not used to compare?
14th Apr 2018, 3:04 PM
Abinash Sarma
Abinash Sarma - avatar