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.
Related
This question already has answers here:
variable access outside of if statement
(5 answers)
What is 'scope' in Java?
(2 answers)
Closed 2 years ago.
I'm new to programming and I thought it would be fun to build a game, but I've run into an issue. I want to make it to where the user can collect an item once but once they collect it they can't collect it anymore. The way I'm seeing if they collect it is by changing the value of that variable from 0 to 1. This is what I've got for the collection, but it doesn't seem to be working. It's saying it can't find the variable yesCookie. I also want to point out that this is just the section that allows you to take it or not, the variable stuff is somewhere else.
if(moldyCookie == 0)
{
String yesCookie = "In a cavity within the wall you see a mold-covered cookie. Bleh. \nYou obtained the Rotten Cookie!\n";
}
else
{
String yesCookie = "There's nothing but an empty cavity in the wall. ";
}
System.out.println("You walk forward towards the painting, moving it aside."+yesCookie+"You return to the door.");
System.out.println();
This question already has answers here:
Java - char, int conversions
(4 answers)
Closed 3 years ago.
I'm trying to determine if my ticket number has a "9" then change the price to 5. This is the code I have. The code itself compiles, but doesn't work as intended. I imagine it's because I'm using a wrong operator. If anyone could let me know how I could change it for it to work that'd be greatly appreciated.
for(int x=0; x<Integer.toString(e1.getNumber()).length(); x++)
{
if(Integer.toString(e1.getNumber()).charAt(x) == 9)
{
System.out.println("The price is $5");
}
}
Thank you! I just had to change 9 to '9'! Its working now!!!
it depends on specific requirements. what if 9 is more then one times in string? what you do in such case? if you are sure that 9 appears once in your ticket price. just you next code.
if (yourString.contains("9")){
yourString = yourString.replace("9", "5");
}
Just had to put '9' so it looks for a char!
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);
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm having trouble writing a program that will give me a YES or NO message given the input I enter for my robot. I want to enter my wheel configuration (doesn't matter what it is), and I want the output to show YES if it can move with the wheel configuration I enter.
Thus far, I have this basic code:
If(a=="yes"){
System.out.println("YES");
}
else if(a == "no"){
System.out.println("NO");
Notice how I've not added numbers into the bracket because I'm not sure how to approach this, and it's the first time I'm really testing myself - and I'm new to Java. Help is appreciated as ever.
Try to change == with equals
if(a.equals("yes")){
System.out.println("YES");
}
else if(a.equals("no")){
System.out.println("NO");
Because you are comparing Strings you should use equals() since that only compares what was written.
== however only returns True if it refers to the same Object.
Do as following:
if(a.equals("yes"))
System.out.println("YES");
else if(a.equals("no"))
System.out.println("NO");
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.