This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have a portion of code that should do that:
1) Take the name of a color from keyboard
2) Give that color to an object in wich i have another object declaration
3) Decrement from 2 to 1 the correspondant line of the array of 2.
In the main class i want to memorize the color in a variable TemporaryColor.
Scanner input = new Scanner(System.in);
String TemporaryColor = input.nextLine();
playerOrd[1].DecrementoSegnalino(TemporaryColor);
playerOrd is a class that contains this method:
public void DecrementoSegnalino(String color) {
SegnalinoScommessaGiocatore.decrementaSegnaliniScommessa(color);
}
SegnalinoScommessaGiocatore have this array:
private int[] numeroSegnaliniScommessa = {2,2,2,2,2,2};
and this method:
public void decrementaSegnaliniScommessa(String color) {
if (color.equalsIgnoreCase("Black") numeroSegnaliniScommessa[0]--;
if (color.equalsIgnoreCase("Blue") numeroSegnaliniScommessa[1]--;
if (color.equalsIgnoreCase("Green") numeroSegnaliniScommessa[2]--;
if (color.qualsIgnoreCase("Red") numeroSegnaliniScommessa[3]--;
if (color.equalsIgnoreCase("Yellow") numeroSegnaliniScommessa[4]--;
if (color.equalsIgnoreCase("White") ) numeroSegnaliniScommessa[5]--;
}
There is a problem passing the string that i write with Keyboard...
If i use this in the beginning:
playerOrd[1].DecrementoSegnalino("Black");
It works!
What's the problem?
Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of
if (fu == "bar") {
// do something
}
do,
if ("bar".equals(fu)) {
// do something
}
or,
if ("bar".equalsIgnoreCase(fu)) {
// do something
}
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed last year.
I want to know how to compare string to a variable.
For example I want to check if the input taken is run and if it's not print that is not a command
class Main
{
public static void main(String args[])
{
System.out.println("a pokrmon appeard");
System.out.println("what would you like to do");
Scanner scan = new Scanner(System.in);
Random genrator = new Random();
String input =scan.nextLine();
int genrate;
genrate=genrator.nextInt(4);
String fail ="failed to escape";
String escaped ="got away safly";
if (genrate <= 2){
System.out.println(escaped);
}
else{
System.out.println(fail);
}
};
I have tried using some methods like if(input=="run") but it doesn't work
Salam (Hello in Muslim) guy.
To compare a string with another string, you need to call the string1.equals(string2) method.
It compares the value of string1 with string2 and if the values match, it returns true otherwise false.
string1==string2 - compares references to objects of type string, not the values of these objects. You can read more about this here How do I compare strings in Java? . Good luck learning Java. (I'm from Russia, I'm writing from a translator)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I am trying to create the following program in which I need to turn the input String's characters into lowercase to ease my work.
I tried using toLowerCase(); first but it didn't work. Then I tried using toLowerCase(locale); and yet I have not succeeded.
public static void Mensuration() {
Locale locale = Locale.ENGLISH;
Scanner inputz = new Scanner(System.in);
System.out.println("Which kind of shape's formula would you like to find out.2D or 3D?");
char dimension = inputz.nextLine().charAt(0);
if(dimension == '2') {System.out.println("Okay so which shape?");
String dimensiond = inputz.nextLine().toLowerCase(locale);
if(dimensiond == "rectangle") {System.out.println("Area = length x breadth\nPerimeter = 2(length + breadth)");}
}
}
I expected the program to give the accurate output but the thing that happens is that there is no output actually!!
use equals to compare strings. I think this causing the error
"rectangle".equals(dimensiond)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I don't understand what's wrong with the IndexOf function ???
public String[] PseudoExisteTest() {
// looking if an XML tag contain "OK"
String exampleText = "<result>OK</result>";
int ind1;
int ind2;
String returnTable[] = new String[4];
String tag="result";
String textresult;
ind1=exampleText.indexOf("<"+tag+">"); // 0
ind2=exampleText.indexOf("</"+tag+">"); // 10
textresult=exampleText.substring(ind1+tag.length()+2, ind2);
if ((textresult=="OK")) { // YES => Normally we pass here (="OK") !
returnTable[0]="It'OK";
}
else {
returnTable[0]="Not, value is : "+textresult+"!"; // Not, value is : OK !!! ?????
}
returnTable[1]="blabla";
return returnTable;
}
The value is "OK" but on the condition, that's don't works well ????
Is anybody can help me ?
Thanks in advance.
The problem is that you're using == to do a comparison of Java Strings. For objects in Java, which includes Strings, == tests whether the objects are the same. Instead, say textresult.equals("OK") or textresult.equalsIgnoreCase("OK").
As the comments say, see also How do I compare strings in Java?
This question already has answers here:
How do I compare strings in Java?
(23 answers)
String.equals versus == [duplicate]
(20 answers)
Closed 9 years ago.
There seems to be a difference between main(String[] args) and other string arrays that i can not figure out, my example.
public class TestArgs
{
public static void main(String[] args) {
String[] x = {"1","2","3"};
System.out.print( x[2] == "3" );
System.out.print( args[2] == "3" );
}}
I run this program as:
java TestArgs 1 2 3
I would expect the output to be "truetrue" but instead I get "truefalse"
Could someone please tell me what the difference is, or am I just doing something really stupid...
in java, you have to use "test".equals("test") to test for string-equality ;)
strings are objects and the objects are not the SAME, they just have the same VALUE
That is because you're comparing the reference of the objects when you use ==. When you're comparing String, use .equals() instead of ==. This SO answer better explains why.
So your code would become something like this:
public class TestArgs {
public static void main(String[] args) {
String[] x = {"1","2","3"};
System.out.print("3".equals(x[2]);
System.out.print("3".equals(args[2]));
}
}
Also, and this is not related directly to this answer, it is always a good idea to check the length of your args before doing any operation using that. The reason is that the end user might not have provided any value for args[2]
The == operator compares objects by reference.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
public void play () {
int anInteger;
//guess return code
int code;
while (true) {
String input=null;
input = JOptionPane.showInputDialog("Please enter an integer");
if (input == "-1") {
//JOptionPane.showMessageDialog(null, input);
System.exit(0);
break;
} else {
if (input==null) {
System.exit(0);
} else if (input.isEmpty()) {
continue;
} else {
anInteger = Integer.parseInt(input);
code = this.oneGuess (anInteger);
//JOptionPane.showMessageDialog(null, anInteger);
}
}
}
}
I want, if the user enter -1, show the program will not prompt the message box any more. Above is the code I have come up with, so far. Why it doesn't work?
String comparisons does NOT work with "==" operator, use "String.equals(Object)" function
input.equals("-1");
Better way would be
"-1".equals(input);
as it also takes care of null input
You are comparing strings, which are objects, with the == operator, which checks whether two object references refer to the same object instance. Instead you should use the equals method for comparing them.
There is a difference between comparing with == and equals. The first compares pointers, the latter contents. That is probably your issue.
You compare Strings with ==, which creates a problem. You can have many different String-Objects which all show "-1". The == tests, if you have exactly the same object on the left and right side. You want to know, if the objects on the left and right sie have an equal content.
Better try
input.equalsIgnoreCase("-1");
EDIT: To answer the comment: input.equalsIgnoreCase("-1") is the same as input.equals("-1") in the case of "-1" as there are no uppercase/lowercase letters in "-1". However, I prefer equalsIgnoreCase in the case of Strings, because it is defined on String, rather than on Object. Still, as the equals-definition is overridden for the String class, it works too in this example and "ignoreCase" is not needed.