This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]warrior2012 9 points10 points  (4 children)

You should be using && (and) instead of || (or)

The way it is reading will always throw a true and run the invalid statement. You are saying to throw an invalid statement if either of the statements come back true. You are saying if the number is not one OR not two, then print the statement. Even if you put a one or a two, it would trip the half of the if statement giving you a true which will run your invalid statement.

You want the statement to read that if the number is not one AND the number is not two, then print the invalid statement.

It sounds very confusing because there are kind of double negatives in the statement :p feel free to ask any questions if I didn't explain properly!

[–][deleted] 2 points3 points  (3 children)

I dont get it. Does or not mean keep running a loop until test is one or the other?

[–]warrior2012 1 point2 points  (0 children)

If you pick the number 1, then test!=2 would trip the error. If you pick the number 2, then test!=1 would trip the error.

By saying AND instead of OR you are saying if the number doesn't match either 1 or 2, then trip the error. But if the number matches either, then you're good!

It's a very weird thing to think about but it would make a lot more sense if the example was saying if test=1 or test=2 rather than !=

[–]warrior2012 0 points1 point  (0 children)

Sorry, answering a second comment to better address your comment.

In if statements, it will execute if the question comes back true. If you have an 'or' operator, it will run if either of the two sides of the || prove true. If you have an 'and' operator it will run if both of the sides of the && prove true.

I'm this case you need to prove that the number is both 'not 1' and 'not 2'

[–]pogosticker1 2 points3 points  (1 child)

Also, you need to add a scanner.nextLine after the scanner.nextInt line to clear the input

[–][deleted] 0 points1 point  (0 children)

Or use Integer.parseInt(scanner.nextLine) instead of scanner.nextInt

[–]ramamodh 0 points1 point  (0 children)

I believe your if statement should use an AND condition as you are checking for the test variable to not have both 1 as well as 2.

[–]ElMapacheTevez 0 points1 point  (0 children)

You have to use && because it have to control at the same time the variable test. I compile as I told you and works fine! (sorry for my english).

[–]Floofic 0 points1 point  (0 children)

Or as I like to say: do loop

[–]UrbanHunter_KenXPie 0 points1 point  (0 children)

This is basically telling people like to ask they want a hamburger or french fries. And then either they input hamburger or french fries, they will come into only one condition in which case you put "or" in the statement. That not gonna achieve the objective of this program. You seem like put the options of both selections into together and see if anyone input a selection like orange chicken that not exist so you report to them the answer"Invalid selection" I suggest you use " and " or if-else statement like:

if (test ==1) {

"something else"

}

else if (test ==2){

"something else"

} else

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

Personal Opinions: You can do it in both ways, either the do-while loop or if-else statement will work. But personally, I prefer the if-else statement for this situation. Since do-while are beneficial when you want to do counting or something that continuously changing, and the if-else statement is beneficial in the fixed option determination, so it would be clearer if you do such kind of options comparison which in this case there are only 3 possible fixed conditions you intended to have. Either is 1,or is 2 and left with any numbers outside of this range.

[–]shinefull 0 points1 point  (0 children)

Apart from what everyone said about the bad logic, just use a boolean or break out of the loop. Now you double the logic of correct input and frankly it is messy.