when i do...

String menu;

Scanner keyboard = new Scanner(System.in);
menu = keyboard;

if (menu = "D"){
   ....
}

there is error sign under "menu = "D"" <can't convert String to boolean>
why menu is boolean? I stated in String....

Recommended Answers

All 4 Replies

when i do...

String menu;

Scanner keyboard = new Scanner(System.in);
menu = keyboard;

if (menu = "D"){
   ....
}

there is error sign under "menu = "D"" <can't convert String to boolean>
why menu is boolean? I stated in String....

There are a number of issues with the above code.

First of all, when declaring a variable (like menu), you should initialize it on the spot... even if it is a null initialization.

Secondly, you are attempting to assign menu (which is a String type) to a Scanner type with the expression menu = keyboard. You probably meant to do something like--

menu = keyboard.nextLine()

-- such that you will receive a block until input is entered in the Scanner (in this case, from the Standard InputStream which is your keyboard).

The next seriously incorrect statement is the expression in the if statement. The problem is that it will always evaluate to be true because what you're asking the compiler to interpret is if the assignment of "D" to menu was not a null assignment, and since "D" is a valid String and menu can hold Strings, it will evaluate to be true.

You probably meant to do something like--

if(menu == "D")

-- such that there will be a boolean return based on the evaluation of menu being equivalent to "D".

However, this kind of comparison between objects is considered lazy and can result in behavior that is not predictable in a program. For now, when comparing Strings, use the following method--

if(menu.equalsIgnoreCase("D"))
commented: All relevant and more help than asked for. +12

Hello there

I am facing a bit similar problem over here.
What i try to achieve is Search for specific keywords like AND, OR in the user's input.

So as far as i've read we get information from the user via Scanner(System.In) as :

Scanner asdfg123 = new Scanner(System.in);

But how can I look into the input ?

I mean check if the second word is AND, OR
Find out if i received 3 or 4 words

Help greatly appreciated.
Beginner.

hatux, please start a new thread when you have a question of your own. This thread belongs to enuff4life concerning the original post.

I know this thread has been dead for a while, but Alex Edwards' code really just helped me out. I couldn't see the problem with this code:

    Scanner scan = new Scanner(System.in);
    String reply = scan.nextLine();
    if(reply == "yes")
    {
        // do something special
    } else {
        // don't do anything special
    }

For some reason, when I entered "yes," the if(reply == "yes") bit never evaluated to true. I changed the code as follows and it worked like a charm:

    Scanner scan = new Scanner(System.in);
    String reply = scan.nextLine();
    if(reply.equalsIgnoreCase("yes"))
    {
        // do something special
    } else {
        // don't do anything special
    }

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.