Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7604905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:02:00+00:00 2026-05-31T00:02:00+00:00

I’m new at Java. I’m looking for some help with homework. I wont post

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:02:02+00:00Added an answer on May 31, 2026 at 12:02 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am doing a simple coin flipping experiment for class that involves flipping a
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.