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....
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....
The compile error you saw comes from two separate mistakes in the original code: a type mismatch and using the assignment operator where a boolean expression is required. tried to put a Scanner into a String variable and then put an assignment inside an if. In Java an if(...) must evaluate to a boolean; assigning a String value produces a String, so the compiler refuses to convert it to boolean. was right to say you must read text from the input and then compare the resulting String, but the idea that the assignment would “evaluate to true” is a C-style notion — Java will not accept assigning a non-boolean inside an if.
Safer patterns:
Example using a simple line reader and a null-safe compare:
import java.io.*;
String line = new BufferedReader(new InputStreamReader(System.in)).readLine();
if ("D".equals(line != null ? line.trim() : null)) {
// handle command D
} To detect keywords and count words (answering ): split the input on whitespace and inspect tokens:
String[] parts = (line == null) ? new String[0] : line.trim().split("\\s+");
int words = parts.length;
if (words >= 2 && "AND".equals(parts[1].toUpperCase(java.util.Locale.US))) {
// second token is AND (case-insensitive)
} Extra tips: avoid == for String content comparisons (it checks object identity), always trim user input, guard against null, and close readers when done. ’s experience shows case-insensitive comparison fixes many user-input problems, but prefer the null-safe and locale-aware patterns above for production code.
Jump to Post— Alex Edwards 321when 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 …
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")) 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!
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.