r/learnjava • u/Super_Good9981 • 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
3
u/anonymz007 Feb 25 '24
Where have you initialised scanner?
I m a newb. Just clarifying 😄
-2
u/Super_Good9981 Feb 25 '24
System.out.print("121: ");
int age = scanner.nextInt();
4
u/anonymz007 Feb 25 '24
Import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
I added this... And the code worked ... It takes the next integer value entered in the CLI.
-1
u/Super_Good9981 Feb 25 '24
I wanted to enter values to get terms like child, adult, or other terms.
2
Feb 25 '24
You have to do both. You create a scanner like poster above Said . You want that Scanner scanner = new Scanner() at top of your code and then thr scanner.nextInt() in your code is what's actually using it
3
u/BannockBnok Feb 25 '24
Your post doesn't have enough information for us to actually assist you. You've just pasted a small fraction of the code from your IDE without any context or details. "It's not working. What do I do?" is a horrible way to ask others for help. Give us all of your code, and upload it to pastebin with formatting enabled. Then, tell us what you're expecting the code to do and compare it to what it's currently doing.
2
u/Kittensandpuppies14 Feb 25 '24
And tell us what the actual error message is
1
u/Super_Good9981 Feb 25 '24
public class LifeStageIdentifier {
public static void main(String[] args) {
// Create a Scanner object to read input from the keyboard
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the person's age
System.out.print("21: ");
// Read the input as an integer
int age = scanner.nextInt();
// Determine the person's stage of life based on age
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");
}
// Close the Scanner object
scanner.close();
}
}
1
u/Super_Good9981 Feb 25 '24
I wanted to print terms like Adult when i entered 21 and it just printed 21
1
-1
u/Kittensandpuppies14 Feb 25 '24
Go through it line by line or with a debugger. It’s literally printing 21 before it does anything else and doesn’t prompt the user for anything. Pro tip: try harder next time
1
1
u/ganked_it Feb 26 '24
What error are you getting
1
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.
1
u/NotYetaProgrammer Feb 25 '24
You should input the age number in the console. Initiate the first sout with "Enter age" instead of "40".
0
u/mi6oka27 Feb 25 '24
first of all initialise the Scanner object, after that check your if statement logic
1
0
0
Feb 25 '24
[deleted]
0
u/nekokattt Feb 25 '24 edited Feb 25 '24
how can an age be both less than zero and greater than 120 at the same time?
This is not their issue at all and the OR is perfectly valid there. Their code will be outputting something (even if it is totally wrong) if they run it. If they are getting zero output at all then it is a totally different issue and they have omitted some code if this thing actually compiles.
Please don't give confusing advice... and thanks for the downvote.
2
u/Super_Good9981 Feb 25 '24
If the age is less than 0 or greater than 120, print "Invalid age".
This is what i was trying to solve using if else statement
3
u/nekokattt Feb 25 '24
yes, and that is totally fine. The deleted comment was saying your logic is wrong but that is false. You did it correctly.
The comment was
bruh your logic is wrong your first if needs an AND rather than an OR to execute
which was nonsense.
The issue is you have a lot of missing code in your example. You have no main class, no main method, no import for the scanner class and you do not define the scanner object, so we can't help further.
•
u/AutoModerator Feb 25 '24
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full - best also formatted as code block
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.