java string validation for 3 choices [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 6 years ago.
I'm trying to make a user validation for multiple choices using a while loop and If the user doesn't match the choices then the program has to terminate. This is what I got so far
while(input!="air" || input!="water" || input!="steel"){
System.exit(0);
}
Unfortunately, it doesn't work.
I would prefer to do exceptions, but my professor is strictly against using it for this assignment. I really want to grow as a programmer so any advice, tips and tricks are welcomed, thank you all for trying to help me out.

Change it as
while(!input.equals("air") && !input.equals("water") && !input.equals("steel")){
System.exit(0);
}
Or
while(!(input.equals("air") || input.equals("water") || input.equals("steel"))){
System.exit(0);
}

Related

What does this statement in Java mean? [duplicate]

This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 4 years ago.
I am confused by this statement. Please help me explain this statement:
return third == Long.MIN_VALUE ? (int)first : (int)third;
Considering your top tags in SO is Python, let's explain this statement with Python:
return first if third == sys.maxint else third
Of course the Long.MIN_VALUE is not necessarily equal to sys.maxint in Python.

Simple check operation is not working in java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String TypedMaxNumber = MaxValue.getText();
if(TypedMaxNumber == "100")
System.out.println(TypedMaxNumber+" = 100");
I think it is a silly problem, but when I am running to run this program and I type 100 in the text Field it is not going inside the loop. What could be the reason.
I am running to run this program and I type 100 in the text Field it
is not going inside the loop.
Its
if(TypedMaxNumber.equals("100"))
Since TypedMaxNumber is of type String. equals() check for value equality

Weird issue with String [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I am working on a multiplayer game.. and i came up with a really weird situation.
It's even possible? -
System.out.println(foo); // left
System.out.println(foo instanceof String) // true
System.out.println(foo == "left") // false
I don't understand how it's even possible..
I really don't think it will be a good idea to post all of my code.
Any ideas why its like that? and how can i fix it?
Thanks in advance
You shouldn't compare Strings by == operator, use equals method instead.

How can I do a presence check in Java? i am getting an error. [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
i am trying to do a presence check in java on 10 JTextFields. I want it so that if all 10 of my textfield have something in them, it will do my code.
String input1 = tfQ1.getText();
String input2 = tfQ2.getText();
etc.
I have put
IF(input1==("")&&input2==("")&&input3==("")&&input4==("")&&input5==("")&&input6==("")&&input7==("")&&input8==("")&&input9==("")&&input10==(""))
{
//DO SCORES ETC
}
However, this doesnt do anything... (my button does not work weather there are things in the text fields or not)
Please and someone help with presence check validation? Thanks =)
instead of "==" operator you should use
input1.equals("")
if(input.equals("WhateverYouAreLookingFor")) {
//do this
}else {
//do this
}
== is a reference comparison, both objects point to the same location in memory. essentially it tests wether the two operands refer to the same object.
.equals() will only compare what is in the String. It can be overridden so two distinct objects can still be equal.

How could I fix this side effect? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I am having a problem with the code below; the issue is that after you don't say map the first time, it keeps repeating No try again with the input box, no matter if you type "map" or not.
System.out.println("My cousin Diego is in trouble in the Majestic palace. But how do we get there?");
System.out.println("Who do we call when we don't know where to go? Huh? I didn't get that...");
imTheMap=robotMagic.next();
if (imTheMap.equals("map"))
{
System.out.println("That's right!"); //the issue is here
}
else
{
while(imTheMap!=("map"))
{
System.out.println("No! try again");
imTheMap=robotMagic.next();
}
}
By changing
while(imTheMap!=("map"))
to
while(!imTheMap.equals("map"))
you always should use equals() method to check string equality. as in your if statement.

Categories