Tell me the Output of the code please | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Tell me the Output of the code please

import java.util.Scanner; class MyClass { public static void main(String[ ] args) { Scanner myVar = new Scanner(System.in); int age =myVar.nextInt(); System.out.println(age); if (age < 16) { System.out.println("Too Young"); } else { System.out.println("Welcome!"); } } }

1st May 2020, 1:18 PM
Ashish Pal
Ashish Pal - avatar
10 Answers
+ 3
It is JavaScript?
1st May 2020, 1:23 PM
Abhay
Abhay - avatar
+ 2
It is Java and not JavaScript. You cannot have a variable name with space in between. So 'my var' is not a valid name, instead you could use 'myVar'. Also if you want to take an integer input then you must write- int age = myVar.nextInt();
1st May 2020, 1:34 PM
Avinesh
Avinesh - avatar
+ 2
There are errors on your program •name of your scanner should not have spaces. •you did not declare variable age. •my var.nextLine(); should be age=myvar.nextInt(); because you are pertaining to the age. •because it's a user input type you can't tell the output unless you have constant value for age.
1st May 2020, 2:31 PM
tres
+ 2
It is not JavaScript its Java, There are some errors in the code :- 1) The "my var" is not valid it must be without space "myvar" 2) You must add "int age = myvar.nextInt();" before if statement. 3) After above given changes the "System.out.println(myvar.nextInt());" will cause error, change it to "System.out.println(age);" and type it after declaration of variable age. And you can't tell the output before the user input.
1st May 2020, 5:29 PM
Meetesh
Meetesh - avatar
+ 2
//This is the code without errors : import java.util.Scanner; class MyClass { public static void main(String[ ] args) { Scanner myVar = new Scanner(System.in); int age =myVar.nextInt(); System.out.println(age); if (age < 16) { System.out.println("Too Young"); } else { System.out.println("Welcome!"); } } }
3rd May 2020, 2:09 PM
Meetesh
Meetesh - avatar
+ 1
@Avinesh not working bro can you help me
3rd May 2020, 1:57 PM
Ashish Pal
Ashish Pal - avatar
+ 1
King Aap This should be your first 3 lines. Scanner myVar = new Scanner(System.in); int age =myVar.nextInt(); System.out.println(age);
3rd May 2020, 2:01 PM
Avinesh
Avinesh - avatar
0
Ya
1st May 2020, 1:25 PM
Ashish Pal
Ashish Pal - avatar
0
Avinesh thank you for your help It's working now
3rd May 2020, 2:06 PM
Ashish Pal
Ashish Pal - avatar
0
@Avinesh 8 8 Is to young Program finished
3rd May 2020, 2:11 PM
Ashish Pal
Ashish Pal - avatar