Spinner selection not passing into "if" statement [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
What is the difference between == and equals() in Java?
(26 answers)
Closed 8 years ago.
I have the following code:
public void convert()
{
String val = (String) spinner1.getItemAtPosition(spinner1.getSelectedItemPosition());
System.out.println(spinner1.getItemAtPosition(spinner1.getSelectedItemPosition()));
System.out.println(val);
if(val == "mm" || val == "cm" || val == "m" || val == "km")
{
System.out.println("got into if statements");
initiateLengthConvert();
}
System.out.println("never got it");
}
Now when the first to print statements print out, if I selected "mm", then it prints out:
mm
mm
never got it
Why won't it pass into the if statement? "val" and the possibilities match up, so it doesn't make any sense to me.

Use .equals to compare strings instead of ==.
if(val.equals("mm") || val.equals("cm") || val.equals("m") || val.equals("km"))
Read
What is the difference between == vs equals() in Java?
Also instead of System.out.println("..."); use Log

Try this..
== always just compares two references. String you should use .equals("")
if(val.equals("mm") || val.equals("cm") || val.equals("m") || val.equals("km"))
{
System.out.println("got into if statements");
initiateLengthConvert();
}

Related

Exit array for loop Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
Consider my code below:
System.out.println("Insert your inventory");
for (int i = 0; i<20;i++) {
System.out.print(i+1+".");
if (inventory[i] == "N" || inventory[i]=="n") {
break;
}
inventory[i] = s.nextLine();
}
How can I exit from this loop if the user enters 'N' or 'n'?
You're comparing string with == operator. It does not yield correct result because your constant string "N" and your input "N" do not have same reference/pointer.
You have to use equals() to guarantee the correct compare result between strings.
Replace
if (inventory[i] == "N" || inventory[i]=="n")
With
if (inventory[i].equals("N") || inventory[i].equals("n"))
You should compare your String variables with the .equals() method instead of the == operator.
An explanation about why this is important can be found here on StackOverflow.

My jsp page only executes the else statement [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I have the following login codes.
if (uname == "Abigail" && password=="Abby14"){
response.sendRedirect("http://localhost:8080/Practical_4/member.jsp");
}
else {
response.sendRedirect("http://localhost:8080/Practical_4/index.html");
}
I realized that my jsp page treats the if-statement as if it's an else statement, and only executes the else-statement.
What you do is comparing addresses where strings are stored and not the strings them selfs in some case java will store same string in same address but you cannot count on that.Here is a code example that should explain the issue
public static void main(String... args) {
String a = "a";
String b = new String("a");
String c = "a";
System.out.println(a==b); // false
System.out.println(a==c); //true
System.out.println(a.equals(b)); // true
}
So the buttom line always use equals insterad of ==
Use equals for string comparison.
if (uname.equals("Abigail") && password.equals("Abby14")){
response.sendRedirect("http://localhost:8080/Practical_4/member.jsp");
}
else {
response.sendRedirect("http://localhost:8080/Practical_4/index.html");
}
Hope this helps.
change to use equals and change to order to prevent null pointer
if ("Abigail".equals(uname) && "Abby14".equals(password)) {

JOptionPane not reading the string [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I am trying to use the value of a String obtained through JOptionPane. However, there is a problem with reading the String. What am I doing wrong here?
import javax.swing.JOptionPane;
public class convertNumber123 {
public static void main(String[] args){
String numsystem1;
numsystem1 = JOptionPane.showInputDialog("Please enter the numeral system that you want to convert from: binary, octal, decimal or hexadecimal.");
if (numsystem1 == "Binary" || numsystem1 == "Octal" || numsystem1 == "Decimal" || numsystem1 == "Hexadecimal")
System.out.println (numsystem1 + "it is!");
else
System.out.println ("Please, enter the correct system name.");
}
}
The way you are comparing strings is wrong. In java you have to use the .equals() method like this
if (numsystem1.equals("Binary") || numsystem1.equals("Octal") || numsystem1.equals("Decimal") || numsystem1.equals("Hexadecimal"))
Apparently, I was using == instead of equals() method. That was the problem.

How does the .split() method work in java? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am using Java and have been trying to split my string input into 3 parts. For example, my input will be "AND 1 1", and I am expecting it to go into my if-loop where the condition is parts[0] == "AND". But this is not the case, and I am not sure why.
My code is listed below
Scanner stringInput = new Scanner(System.in);
String input = stringInput.next();
System.out.printf("%s\n", input);
String[] parts = input.split(" ");
if (parts[0] == "AND") {
if (parts[1] == parts[2] && parts[1] == "1")
System.out.printf("1\n");
else
System.out.printf("0\n");
}
else {
if (parts[1] == "1" || parts[2] == "0")
System.out.printf("1\n");
else
System.out.printf("0\n");
}
In Java you can not be sure that the string is the object you think it is. For this reason, you should not use == to compare objects, but use the equals function.
if (parts[0] == "AND")
should be
if (parts[0].equals("AND"))
Strings are immutable, so the functions will always return new strings, when they have to do something on them. For this reason, using == will only work in some particular cases, but never when you process them.
You are using stringInput.next() which will not read spaces. So that is the problem.
Use stringInput.nextLine() instead of stringInput.next()
String input=stringInput.nextLine(); is correct when you are working with text that contains space.
Here is the edited code.
Scanner stringInput = new Scanner(System.in);
String input = stringInput.nextLine();
System.out.printf("%s\n", input);
String[] parts = input.split(" ");
if (parts[0] == "AND") {
if (parts[1] == parts[2] && parts[1] == "1")
System.out.printf("1\n");
else
System.out.printf("0\n");
}
else {
if (parts[1] == "1" || parts[2] == "0")
System.out.printf("1\n");
else
System.out.printf("0\n");
}
You probably might have got an ArrayIndexOutOfBoundsException
You are using the String.split() method in correct way. The parts[0] also contains the desired "AND" after using the method.
But the problem is you are checking equality of two strings using "==" instead of equals method. When you use == then it checks whether they are same reference of string while the equals method check the values of the string.
So your checking should be something like this
if (parts[0].equals("AND"))
Use
parts[0].equals("AND") to check equality of `String`
Just to complete the Devolus answers (I can't put comments in comments yet U_U)
== compares the references, it means that is only true if two variables are pointing to the same object
e.g.
Object a = new Car()
Object b = a
Object c = new Car()
(a == b) //true
(a == c) //false
a.equals(b) //true
a.equals(c) //true
Try this piece of code:
if (parts[0].toString().equals("AND")){
if (parts[1].equals(parts[2]) && parts[1].equals("1"))
System.out.printf("1\n");
else
System.out.printf("0\n");
}else {
if (parts[1].equals("1") || parts[2].equals("0"))
System.out.printf("1\n");
else
System.out.printf("0\n");
}
There are several issues:
as JavaTechnical pointed out, you should use stringInput.nextLine() vs stringInput.next()
as everyone pointed out, use .equals() vs ==, here's a thread explaining why: Java String.equals versus ==
test against a known vs an unknown, reorder your test like so "AND".equals(part[0]) should shield you from null pointer
Try using .substring(), for example:
String a = "AND 1 1"
b = a.substring(1,3)
c = a.substring(4,5)
System.out.println(b)
System.out.println(c)
Would print "AND" then "1"

JAVA - Why a == "1" returns false [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
String is not equal to string?
What makes reference comparison (==) work for some strings in Java?
can some one explain me following java code
String a = "1";
if(a == "1") {
//print compare 1 is true;
} else {
//print compare 1 is false;
}
if(a.equals("1")) {
//print compare 2 is true;
} else {
//print compare 2 is false;
}
it results like
compare 1 is false
compare 2 is true
Only explanation i have is that its comparing the memory address not the values itself. But i am not sure. can some please put a light on it. in .Net == operator is overloaded to compare contents of string.
use "1".equals(a) , String is an object so use equals() to compare
I understood that == operator is compare "Is it same object?"
object a is not same object with constant string "1".
so returns false

Categories

Resources