+ 3

How can I make a program with Scanners?

I was trying to make a program that would utilize a scanner to get me to type my age and than decide with if statements if I was too young. How can I do that? this is the code I typed: import java.util.Scanner; public class Program { public static void main(String[] args) { Int age = new Scanner(System.in); System.out.prinln(age.nextInt); if(age > 0) { if(age > 16) { System.out.println("Welcome!"); } else { System.out.prin

24th Mar 2018, 10:52 AM
David Wicker
David Wicker - avatar
3 Answers
+ 4
Your code won't work. You do not need: System.out.prinln(age.nextInt); Scanner input = new Scanner(System.in); int age = input.nextInt(); if(age>0){ *what happens* } else{ *what happens* } Hope this helps :)
24th Mar 2018, 10:56 AM
Deddy Tandean
+ 5
Ways to use Scanner: 1. Scanner s=new Scanner(System.in); int age=s.nextInt(); 2. int age=new Scanner(System.in).nextInt(); 3. int age=new java.util.Scanner(System.in). nextInt();
24th Mar 2018, 1:29 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 2
u must create a scanner object first Scanner s =new Scanner (System.in); then use it to get the value typed in keyboard (an int for ur case) and set that value to a variable : int age = s.nextInt();
24th Mar 2018, 10:55 AM
Farshaad Heydari
Farshaad Heydari - avatar