Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement with integers [duplicate]

Tags:

java

I'm new at Java. I'm looking for some help with homework. I wont post the full code I was doing that originally but I dont think it will help me learn it.

I have a program working with classes. I have a class that will validate a selection and a class that has my setters and getters and a class that the professor coded with the IO for the program (it's an addres book)

I have a statement in my main like this that says

//create new scanner
Scanner ip = new Scanner(System.in);

System.out.println();
int menuNumber = Validator.getInt(ip, "Enter menu number: ", 1, 3);

if (menuNumber = 1)
{
    //print address book    
}

else if (menuNumber = 2)
{
    // get input from user
}
else 
{
    Exit
}

If you look at my if statement if (menuNumber = 1) I get a red line that tells me I cannot convert an int to boolean. I thought the answer was if (menuNumber.equals(1)) but that also gave me a similar error.

I'm not 100% on what I can do to fix it so I wanted to ask for help. Do I need to convert my entry to a string? Right now my validator looks something like:

if (int < 1)
print "Error entry must be 1, 2 or 3)
else if (int > 3) 
print "error entry must 1, 2, or 3)
else 
print "invalid entry"

If I convert my main to a string instead of an int wont I have to change this all up as well?

Thanks again for helping me I haven't been diong that great and I want to get a good chunk of the assignment knocked out.

like image 546
Jeremy B Avatar asked Feb 28 '12 23:02

Jeremy B


3 Answers

if (menuNumber = 1)

should be

if (menuNumber == 1)

The former assigns the value 1 to menuNumber, the latter tests if menuNumber is equal to 1.

The reason you get cannot convert an int to boolean is that Java expects a boolean in the if(...) construct - but menuNumber is an int. The expression menuNumber == 1 returns a boolean, which is what is needed.

It's a common mix-up in various languages. I think you can set the Java compiler to warn you of other likely cases of this error.


A trick used in some languages is to do the comparison the other way round: (1 == menuNumber) so that if you accidentally type = you will get a compiler error rather than a silent bug.

This is known as a Yoda Condition.


In Java, a similar trick can be used if you are comparing objects using the .equals() method (not ==), and one of them could be null:

if(myString.equals("abc"))

may produce a NullPointerException if myString is null. But:

if("abc".equals(myString))

will cope, and will just return false if myString is null.

like image 126
DNA Avatar answered Nov 14 '22 21:11

DNA


I get a red line that tells me I cannot convert an int to boolean.

Thats because = is an assignment operator. What you need to use is == operator.

like image 24
Mahesh Avatar answered Nov 14 '22 20:11

Mahesh


A single equal sign is assignment: you assign value to a variable this way. use two equal signs (==) for comparison:

if ($menuNumber = 1) { 

Update: forgot dollar sign: $menuNumber

like image 24
Hai Vu Avatar answered Nov 14 '22 22:11

Hai Vu