r/learnjava • • Feb 25 '24

Java if else statement

System.out.print("40: ");

int age = scanner.nextInt();

if (age < 0 || age > 120) {

System.out.println("Invalid age");

} else if (age <= 12) {

System.out.println("Child");

} else if (age <= 19) {

System.out.println("Teenager");

} else if (age <= 64) {

System.out.println("Adult");

} else {

System.out.println("Senior Citizen");

}

I wanted to print a specific result but it wasn't coming up, what errors am i making

0 Upvotes

25 comments sorted by

View all comments

1

u/Human_Technology8101 Feb 26 '24

You have to instantiate the scanner object using Scanner scanner = new Scanner(System.in);
To test if that's causing the issue, temporarily asign some value for age variable and try if that's giving you the right results.