This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
What is the correct way to put string in a condition statement?
I tried this, but it won't print x.
String c = "c" ,d = "d";
Scanner in = new Scanner(System.in);
String selection = in.next();
if ( c == selection){
System.out.println("x");
Assume I input c.
Strings in Java are compared using str.equals(<string to compare>);
So in your case it would be if(c.equals(selection)) not if(c==selection)
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Java error "Value of local variable is not used"
(7 answers)
Closed 2 years ago.
I used this code:
Scanner SC = new Scanner(System.in);
String Gender = " ";
System.out.println("....");
String Ans1 = SC.nextLine();
Ans1 = Ams1.toLowerCase();
if(Ans1 == "yes") {
Gender = "male";
System.out.println(Gender);
}
else {
System.out.println("Get out!");
}
I think I used the Gender variable in if statement, but it's showing The Value of the local Variable is not used.
And when I ran, it works but the value of gender is the same as the previous. It means that if I write yes it doesn't show the word male it shows the space I gave.
Please Help!
#Tech Minds Aditya, You have written Ans1 = Ams1 instead of Ans1 = Ans1
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 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)
Closed 7 years ago.
public static void main(String args[]){
System.out.print("Type new, wil display TRUE:");
Scanner sc = new Scanner(System.in);
String userInput = sc.nextLine();
if(userInput=="new"){
System.out.println("TRUE");
}else {
System.out.println("FALSE");
}
}
I have no idea why new not equals to new.
Please give me some hints :)
Use userInput.equals("new") instead.
This explains everything: How do I compare strings in Java?
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have previously asked a question about comparing 2 strings and was told that I should always use .equals.
However, I do not understand why this then works:
String y= "Mary";
String x= "Mary";
System.out.print(x==y);
This will print true, and I do not understand why.
Because those two String(s) have the same reference identity, and that is because they came from the String intern pool. If you were to add a new String() to one of them, like so -
String y= "Mary";
String x= new String("Mary");
System.out.print(x==y);
You would get false.