r/javahelp • u/ChangeGlum • Mar 11 '22
Unwanted Else statements?
This is a Java project I am practicing. I am trying to have it output the user-selected options. However, it keeps outputting all of the options' else statements. How do I prevent this from outputting all of the else statements? Is it possible to write this as a while statement?
import java.util.Scanner;
public class ShadyRestRoom2 {
`public static void main(String[] args)`
`{`
`Scanner input = new Scanner(`[`System.in`](https://System.in)`);`
`int queen = 125;`
`int king = 139;`
`int couch = 165;`
`int invalid = 0;`
`System.out.println("Welcome to the Shady Rest Hotel. Input 1 for a queen bed, 2 for a king, or 3 for a king and a pullout couch.");`
`int option = input.nextInt();`
`System.out.println("Insert (1) for a lake view or (2) for a park view.");`
`int view = input.nextInt();`
`// Selection statements for the queen bed.`
`if (option == 1 && view == 1)`
`{`
`queen = queen + 15;`
`System.out.println("Option " + option + " has been selected. You will be assigned the queen bed with a lake view. That will be $" + queen + ".");`
`}`
`else if (option == 1 && view == 2)`
`{`
`System.out.println("Option " + option + " has been selected. You will be assigned the queen bed with a park view. That will be $" + queen + ".");`
`}`
`else`
`{`
`queen = queen + 15;`
`System.out.println("Did you intend to select the queen bed with a lake view? That will be $" + queen + ".");`
`}`
`// Selection statements for the king bed.`
`if (option == 2 && view == 1)`
`{`
`king = king + 15;`
`System.out.println("Option " + option + " has been selected. You will be assigned the king bed with a lake view. That will be $" + king + ".");`
`}`
`else if (option == 2 && view == 2)`
`{`
`System.out.println("Option " + option + " has been selected. You will be assigned the king bed with a park view. That will be $" + king + ".");`
`}`
`else`
`{`
`king = king + 15;`
`System.out.println("Did you intend to select the king bed with a lake view? That will be $" + king + ".");`
`}`
`// Selection statements for the king bed and a pull-out couch.`
`if (option == 3 && view == 1)`
`{`
`couch = couch + 15;`
`System.out.println("Option " + option + " has been selected. You will be assigned the king bed and a pull-out couch with a lake view. That will be $" + couch + ".");`
`}`
`else if (option == 3 && view == 2)`
`{`
`System.out.println("Option " + option + " has been selected. You will be assigned the king bed and a pull-out couch with a park view. That will be $" + couch + ".");`
`}`
`else`
`{`
`couch = couch + 15;`
`System.out.println("Did you intend to select the king bed and a pull-out couch with a lake view? That will be $" + couch + ".");`
`}`
`}`}
3
u/Poddster Mar 11 '22 edited Mar 11 '22
Think about the logic involved. Imagine I enter Option = 4 and view = 4.
So:
option == 1 && view == 1
option == 1 && view == 2Both of these are false, aren't they? So therefore it'll go into the else case.
option == 2 && view == 1
option == 2 && view == 2Are also false, so we'll go into the else case
option == 3 && view == 1
option == 3 && view == 2Both false, so else case again
Yes, I know that 4 is not a "legal" input for either value, but the same problems happens if I enter 1, 2, or 3. The way you've written the logic will mean those else cases always happen if the other cases aren't hit
Is it possible to write this as a while statement?
This is a non-sequitur. While statements are used primarily to repeat things, not as conditional selection logic. What I suspect you want is:
if option == 1 {
if view == 1 {
....
} else if view == 2 {
....
} else {
print base input
}
} else if option == 2 {
if view == 1 {
....
} else if view == 2 {
....
} else {
print bad input
}
} else if option == 3 {
if view == 1 {
....
} else if view == 2 {
....
} else {
print bad input
}
} else {
print bad input
}Note that you have 3 options and 3 views. Writing it this way is 9 possible combinations. If you had 5 options and 6 views then you'd have 30 combinations, and you'd see that this quickly becomes a long and difficult way to write this code. There are other ways to write it, but I'll let you find out what they are :)
3
u/DeLocrian Mar 11 '22
You already know that each if/else block prints something so how can you avoid that? (Hint: you do not need to have an else after every if)
There is also a lot of repetition in your code. It might be clearer if you stored your message in a string variable and only printed it at the end.
About using a while loop, not sure how that would help since you are not looping through anything. Consider extracting some logic to methods though (for example having a method to figure out which bed type and another method for the view and contruct the final string message from these results)
1
u/ChangeGlum Mar 11 '22
I finally got it. I simply had to nest each of the if statements under another set of their own if statements for the option booleans! Also, if anyone knows how I can properly post code sections onto Reddit posts, then I would really appreciate it!
import java.util.Scanner;
public class ShadyRestRoom2 {
`public static void main(String[] args)`
`{`
`Scanner input = new Scanner(System.in);`
`int queen = 125;`
`int king = 139;`
`int couch = 165;`
`int invalid = 0;`
`System.out.println("Welcome to the Shady Rest Hotel. Input 1 for a queen bed, 2 for a king, or 3 for a king and a pullout couch.");`
`int option = input.nextInt();`
`System.out.println("Insert (1) for a lake view or (2) for a park view.");`
`int view = input.nextInt();`
`// Selection statements for the queen bed.`
`if (option == 1)`
`{`
`if (option == 1 && view == 1)`
`{`queen = queen + 15;
System.out.println("Option " + option + " has been selected. You will be assigned the queen bed with a lake view. That will be $" + queen + ".");
`}`
`else if (option == 1 && view == 2)`
`{`System.out.println("Option " + option + " has been selected. You will be assigned the queen bed with a park view. That will be $" + queen + ".");
`}`
`else`
`{`queen = queen + 15;
System.out.println("Did you intend to select the queen bed with a lake view? That will be $" + queen + ".");
`}`
`}`
`// Selection statements for the king bed.`
`if (option == 2)`
`{`
`if (option == 2 && view == 1)`
`{`king = king + 15;
System.out.println("Option " + option + " has been selected. You will be assigned the king bed with a lake view. That will be $" + king + ".");
`}`
`else if (option == 2 && view == 2)`
`{`System.out.println("Option " + option + " has been selected. You will be assigned the king bed with a park view. That will be $" + king + ".");
`}`
`else`
`{`king = king + 15;
System.out.println("Did you intend to select the king bed with a lake view? That will be $" + king + ".");
`}`
`}`
`// Selection statements for the king bed and a pull-out couch.`
`if (option == 3)`
`{`
`if (option == 3 && view == 1)`
`{`couch = couch + 15;
System.out.println("Option " + option + " has been selected. You will be assigned the king bed and a pull-out couch with a lake view. That will be $" + couch + ".");
`}`
`else if (option == 3 && view == 2)`
`{`System.out.println("Option " + option + " has been selected. You will be assigned the king bed and a pull-out couch with a park view. That will be $" + couch + ".");
`}`
`else`
`{`couch = couch + 15;
System.out.println("Did you intend to select the king bed and a pull-out couch with a lake view? That will be $" + couch + ".");
`}`
`}`
`/*`
`switch(option)`
`{`
`case 1:`System.out.println("Option " + option + " has been selected. You will be assigned the queen bed. That will be $" + queen + ".");
int v1 = 125 + 15;
`break;`
`case 2:`System.out.println("Option " + option + " has been selected. You will be assigned the king bed. That will be $" + king + ".");
`break;`
`case 3:`System.out.println("Option " + option + " has been selected. You will be assigned the king bed and a pullout couch. That will be $" + couch + ".");
`break;`
`default:`System.out.println("You have selected an invalid option. Price has been set to $" + invalid);
`}`
`*/`
`}`}
2
u/MaiGoL7 Mar 11 '22
You are using a starting if/else for the options, and then you are checking again inside each block + checking the view.
Any IDE will complain that the inner option == 1 is always true.
You could change your approach from
if(option ==1 ){ if(option == 1 && view == 1) { // .. } } else { // .. }to
if(option ==1 ){ if(view == 1) { // option == 1 is assumed here // .. } else if ( view == 2){ // .. }} else { // .. }
2
u/D0CTOR_ZED Mar 11 '22
Regarding the formatting, you have the basics down (at least 4 spaces before each line). You don't need the `s. You should also put 4 spaces in any empty lines if you don't want the code block to break there. The breaks would actually be nice between methods but don't make much sense to me in the middle of code blocks.
•
u/AutoModerator Mar 11 '22
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
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.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
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: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) 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.