detecting specific input from user using BufferedReader [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i'm taking user input from
String riddle=b.readLine();
riddle1=Integer.parseInt(riddle);
and obviously, i'm trying to get the user to answer a riddle.
I tried using the following
if(riddle1=="answer")
but that doesn't work

Try this
riddle.equals("answer");
As == doesn't checks whether both strings are equal or not, it compares string's location.
You can click here for more information about it.

Related

How to put a certain string into *if* and check if it is a certain word [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am trying to scan a string and put it in an if like so:
(this is just an example part of the whole program)
Scanner scan=new Scanner();
System.out.print("Enter the word...");
String a=scan.nextLine();
if (a=="Hi")
System.out.println("Hello!")
Else System.exit(0);
So im checking if the user said "Hi" , but it doesnt work like this, I want a way to do it.
I have just started learning java so my questions might seem a little amateur but your answers will help a lot.
Use "Hi".equals(a), == compares identity, not equality.
Also, you should write specifically "Hi".equals(a) and not a.equals("Hi"), because it won't cause a NullPointerException, since a might be null.

How to Compare two strings in android with optimization? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I already tried with string.equal function which will compare two strings, but what will be optimize way of doing same.
Suggestions are welcome.
Hi all got it may help others What's the quickest way to compare strings in Java?
Thanks Saurabh for sharing link.
According to me, You should use the equals() method of the String class to compare Strings.
if (xyz.equals("something")) {
//Do what you want if string is equal...
}
see this also..

Comparing strings in onPostExecute (resulting from mysql operation) [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I have been trying to connect to a mysql database and it was succesfull. In the onPostExecute method, I get a result which seems to me exactly the same as the real input. In this example the emailadress jb#jb.jb. Logcat shows both the real input as the result. Yet my code doesn't agree that it is the same. Any advice?
EDIT: This link apparently gave the answer: answer on the question
You have to use String.equals() instead of == to compare Strings. check this link

If statement getting true value [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
My program is using the following code
String mobile="";
if(mobile!="")
System.out.println("++++++");
System.out.println("------");
But if statement always set true. how it is possible? how to correct the code to get false for if condition?
try this way
if(!(mobile.equals("")))
there are difference between == and .equals()
As whoAmI has suggested its better to
if(!("".equals(mobile)))
because it can handle mobile as null
you can't use "!=" on strings. This only compares the references of the strings. If you want to compare the content of the strings you need to use equals
if(!("".equals(mobile)))

Comparing strings - Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I'm new to Java. And I've following problem:
string s = "someword";
if (s == "someword")
// do something
Sometimes doesn't work for me. Don't know why.
Thanks for responds.
In Java == compare reference. Use .equals() for compare value.
Duplication of this: How do I compare strings in Java?

Categories

Resources