This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
This is the code I have right now and can't figure out why this while loop isnt ending when there is a period entered.
while (emailTxt!="."){
emailTxt = "";
emailTxt = scanner.nextLine();
totalText.add(emailTxt);
}
emailTxt is a String. A String in Java is considered an Object. You want to use the Object.equals().
So
while(!(emailTxt.equals("."))
You should use equals() to compare two string.
while (!".".equals(emailTxt)) {
// ...
}
Related
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
I have a delimited string and The delimiter is "$%". The fields are in an specific order. For example: John$%Smith$%30$%Los Angeles
I need to get the values of this string and store them in the respective property
Customer.firstName(_)
Customer.lastName(_)
Customer.age(_)
Customer.city(_)
So far I have tried this but I know I am doing it incorrectly:
if(thisString != null){
if(thisString.endsWith("$%")){
Customer.firstName(thisString.substring(0,indexOf("$%");
}
}
Trying using Java's String.split
For example:
//Split string based on "$%"
String[] values = thisString.split(Pattern.quote("$%"));
Customer.firstName() = values[0]; //Set first name equal to first string from the split
//etc
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm trying to write a simple hangman game in java for my college class. For some reason the if statement never returns seems to think that the two substrings being compared are equal. The two print statements show that by all rights the two should equate.
public String guessLetter(String letter)
{
String blanks = "";
String theWord="FOO";
for(int i=0; i<=theWord.length()-1; i++)
{
System.out.print(letter.substring(0,1).toUpperCase());
System.out.print(theWord.substring(i,i+1)+ "\n");
if((letter.substring(0,1).toUpperCase())==(theWord.substring(i,i+1)))
{
blanks = blanks + theWord.substring(i,i+1);
}
else
{
blanks = blanks + "___ ";
}
}
return blanks;
}
EDIT - As a great many people have pointed out, when comparing Strings, one must use the equals method instead of ==. I was unaware.
You are comparing a String so use "String".equals() dont use ==
use like this:
if((letter.substring(0,1).toUpperCase()).equals(theWord.substring(i,i+1)))
Java dont have == for string
you must use string1.equals(string2) function
if((letter.substring(0,1).toUpperCase())==(theWord.substring(i,i+1))) \ this is wrong for strings
When you compare strings you should use .equals or .equalsIgnorecase
if((letter.substring(0,1).toUpperCase()).equals(theWord.substring(i,i+1)))
ans also checkout the difference between == and .equals in java good explanation is given there.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm getting the user input like so:
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
When I compare it like this
if(input == "abc"){
System.out.println("Match!");
}else{
System.out.println(input + "\nabc");
}
If I type "abc" I see
abc
abc
Instead of the expected match. What's going on?
You are comparing String references, not String values. Use the String.equals method instead.
== is comparing memory locations. you need to use input.equals("abc") instead.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
String s1 = "abcdef";
String s3 = new String(s1);
if(s3 == s1){
System.out.printf("yes");
}else{
System.out.printf("no");
}
Why is not print yes? Who can explain to me?
Change s3 == s1 to s3.equals(s1).
The == operator or will check if they're both the same object, rather than what their string value is.
When you are dealing with Objects, you should use their equals method, not the ==
Check this for further explanation:
Java String.equals versus ==
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have an if statement that takes a string, and if another string has the same value as that string do 1 thing, and if the variable doesnt equal that string do another thring
here is my code
if(Pos != "D"){
System.out.println("doesnt = D");
}
if (Pos == "D" ){//WHY ISNT THIS WORKING
System.out.println("it does = D");
}
It recognizes when the variable doesnt = D and prints "doesnt = d" but when the variable = D it does nothing. I dont know why.
thanks
Never compare Strings with == or != since these check to see if two String variables refer to the same object reference, and this is not what you're interested in. Instead use the equals(...) or equalsIgnoreCase(...) method to see if the two Strings have the same chars in the same order as that's what really matters here. i.e.,
Use equals to compare strings :
if ("D".equals(Pos))