This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
New to Java, taking college course.
I am writing a program that asks users for their internet package so it can calculate their bill. Here's the snippet where I ask for their package, and have to confirm that they have answered either A, B, or C.
For some reason, it enters the while loop even if I enter "a"..
//Create Keyboard scanner
Scanner keyboard = new Scanner(System.in);
//Get user package
String servicePackage;
System.out.println("Enter your internet package: A, B, or C");
servicePackage = keyboard.nextLine();
servicePackage = servicePackage.toUpperCase();
System.out.println(servicePackage);//This line is for debugging
//Validate User Package Input
while (servicePackage != "A" && servicePackage != "B" && servicePackage != "C"){
System.out.println("Please enter a valid internet package (A, B or C)");
servicePackage = keyboard.nextLine();
servicePackage = servicePackage.toUpperCase();
System.out.println(servicePackage);//for debugging
}
System.out.println(servicePackage);//for debugging
Not sure if it's relevant, but I am using jGrasp.
Any help is appreciated!
"string" (declared) == "string" return false. You should use equals ("string".equals("string") return true) or equalsIgnoreCase (for example: "STRING".equalsIgnoreCase("string") return also true)
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 6 years ago.
I am creating a program that simulates Rock, Paper, Scissors, and I need the user to input either "Rock","Paper", or "Scissors". My Code is:
Scanner input = new Scanner(System.in);
String userIn = null;
while ((userIn != "Rock") || (userIn != "Paper") || (userIn != "Scissors")) {
System.out.println("Please Type either: Rock, Paper or Scissors");
userIn = input.next();
}
I created a scanner for the input and set the initial input to null. When the program runs, since the input is not either "Rock","Paper", or "Scissors", it will prompt the user to enter one of those three, the problem is even when I enter "Rock","Paper", or "Scissors" correctly, it still reprompts me to "Please Type either: Rock, Paper or Scissors".
what am I doing wrong?
For string comparisons use
userIn.equals("yourString") function rather than != comparisons.
You should try using .equals() or better .equalsIgnoreCase() if you are not bothered about the case of the input string. That might help.
equal or equalsIgnoreCase uses the content of object to make compare,but != do the comparison with the reference address
so the code should be
while ((userIn.equalsIgnoreCase("Rocker")) || (userIn.equalsIgnoreCase("Paper") || (userIn.equalsIgnoreCase("Scissors"))
Update your program like this,
Scanner input = new Scanner(System.in);
String userIn = "";
while (!(userIn.equals("Rock") || userIn.equals("Paper")|| userIn.equals("Scissors"))) {
System.out.println("Please Type either: Rock, Paper or Scissors");
userIn = input.next();
}
firstly, if you are doing
String userIn = null; //instead String userIn = " ";
we can't check
userIn.equals("Rock"); // i.e. null.equals("Rock");
compiler will through a null pointer exception. So, edit your program as above and will work properly.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I am not sure why but when I get a string from the user, I cannot compare it in an if statement but when I try to print it, it works fine.
Part of my code:
Scanner in = new Scanner(System.in);
while (true) {
String userInput;
int rowInput, colInput;
printBoard(board);
System.out.print("Move: ");
userInput = in.next();
// shift board right on a row
if (userInput == "r") {
System.out.print("row #: \r");
rowInput = in.nextInt();
moveRight(--rowInput, board);
}
Does anyone know why this isn't working as expected?
You an try this:
if (userInput.equals("r"))
== is used to compare the address and equals is used to compare contents.
I should be using equals instead of ==.
So it would lead to:
...
if (userInput.equals("r"))
...
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)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm currently working on a financial planning app for class but I cant get a loop with a condition inside it to work. It just keeps looping despite the condition - it's almost as if the condition is being ignored completely.
Here's my code - please help!
while (true){
Scanner scanVar = new Scanner(System.in);
System.out.println("\nEnter expenditure item: ");
String myString = scanVar.nextLine();
Scanner scanVar2 = new Scanner(System.in);
System.out.println("\nEnter expenditure value: ");
double myDouble = scanVar2.nextDouble();
expenditureMap.put(myString, myDouble);
Scanner scanVar3 = new Scanner(System.in);
System.out.println("\nAnother item? ");
String myString2 = scanVar3.nextLine();
if (myString2 == "yes") {
continue;
}
else {
break;
}
}
Many thanks,
Dylan
You really want to be using mystring2.equals("yes") (or even better, "yes".equals(mystring2) )
The == operator on objects tests for them being the identical instance, not the same string values....
String a = new String("yes");
String b = new String("yes");
a == b => false
a.equals(b) => true
If you are using the == operater it is comparing if the object references match. You should use the equals operator
if (myString2.equals("yes"))
change the condition as follows and then try:
if (myString2.equals("yes")) {
You shall use equals ... check this post
How do I compare strings in Java?
reference comparison means checking if both objects have the same address in memoery
value comparison means checking the value inside the objects