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.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String play = "y";
System.out.print("Enter something: ");
play = scan.next();
System.out.println(play);
if (play == "Y" || play == "y")
{
System.out.println("If test works!!");
}
System.out.println("Did it work???");
}
}
I assume this has something to do with when I press enter, it's storing that as well. I tried changing String play to a char, but then I get errors from Scanner saying it can't change a String to a char.
You should atmost avoid using “==“ when comparing objects especially strings. “==“ checks for object references. Change the comparison to use .equals method and it should work
if(play.equals(“Y”) || play.equals(“y”))
in case if “play” can be null, the below snippet is more safe.
if(“Y”.equals(play) || y.equals(play))
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.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
Whenever the user input is 1 or Jan, the code does not read and jump to second statement and display
Invalid month has been entered
Scanner in = new Scanner(System.in);
System.out.println("Enter a month: ");
String month=in.nextLine();
if((month == "1") || (month == "Jan")){
System.out.println("Month: January");
}
else{
System.out.println("Invalid month has been entered");
With Strings, use "equals" instead of ==.
if(("1".equals(month)) || ("Jan".equals(month)))
This is needed because Strings are not primitive types, but a special kind of Objects in Java.
You can check the following for more info:
What is the difference between == vs equals() in 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();
}
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.
package temperatureconversion;
import java.util.Scanner;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter Conversion type: Press C for Celsius to Fahrenheit or press F For Fahrenheit to Celsius.");
String Vctype = keyboard.next();
if (Vctype == "f" || Vctype == "F"){
System.out.println("Please enter fahrenheit");
double Vfahrenheit = keyboard.nextInt();
Vfahrenheit = (Vfahrenheit)*(9/5)+(32);
System.out.println(Vfahrenheit);
}
if (Vctype == "c" || Vctype == "C"){
System.out.println("Please enter celcius");
double Vcelcius = keyboard.nextInt();
Vcelcius = (Vcelcius - 32)*(5/9);
System.out.println(Vcelcius) ;
}
}
}
Hello guys I was wondering if anyone could help me with the above code. Basically in the output console in netbeans the program just seems to end after I hit C or F, but instead it should ask for a number then allow a number input, then calculate and finally display the calculation. It doesn't seem to be executing the if statements Where am I going wrong?
You are comparing String with ==. So it doesnt work, you have to use this :
if (Vctype.toLowerCase().equals("f"))
Note also, that using a "toLowerCase" makes the whole string lowercase, so you dont have to have two options for "F" and "f".
If you want, you can use "compareTo"
if (Vctype.toLowerCase().compareTo("f") == 0)