This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am reasonably experienced with programming, but not specifically in Java. I am coming across the following error when working in eclipse. My code is the following:
I have used the debug function, and it reports that carbonPrefix is pent, but that carbon stays at 0 throughout. Like I said, I am a novice to Java and Eclipse, so I may not be using the debug function to it's full extent.
For anybody that's interested, this the start of code where you input the name of an alkane and it tells you the formula. It worked in Javascript and I'm just trying to translate it into Java.
Thank you all so much!
you have to use
carbonPrefix.equals("pent");
in java == operator used to compare two object references and the method equals() is used to compare two strings to determine whether they are equal or not.
Related
This question already has answers here:
Fuzzy string search library in Java [closed]
(8 answers)
Closed 6 years ago.
I am working on a game where the user has to fill in a name of a celebrity. If the name is not 100% correct but nearly correct, the compare should succeed. Is there a ready to use function in java or something someone ever has written so I can use it ?
What you are looking for is the Levenshtein algortithm
You'll find here some Java implementations : https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Java
Or, if you don't want/need to understand how it works, you can get the score directly from Apache StringUtils : getLevenshteinDistance
And if you want to get the percentage of similarities, you can do :
int lev = StringUtils.getLevenshteinDistance(s1, s2);
double ratio = ((double) lev) / (Math.max(s1.length, s2.length));
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
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
EDIT: Thank you, PakkuDon
instead of using "==" I must use ".Equals()"!
I'm trying to implement a chat-command system though I've encountered troubles in differentiating between standard chat and command chat..
Focusing on just one command for now, `highlight
the output whenever I type in `highlight is:
highlight
`highlight
Here's my code:
String cmd = InMessage.message.substring(0, 10);
System.out.println(cmd);
System.out.println("`highlight");
if( cmd == "`highlight" )
{
... cancel chat packet and proces command
}
and yet the if statement returns false.
What's going on here? Anything I've done is wrong?
You have a number of problems. Firstly, that substring is going to throw an exception if the user types something that's less than 10 characters. Secondly, you're using == where equals would work better. But you don't need either of these things. You could just use
if (InMessage.message.startsWith("`highlight")) {
// whatever
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Raw Strings in Java?
In C# there is such a thing as # ("at sign") that can be put before string if forbidden symbols occurs. For example:
#"a\b\c"
In java I have to put backslashes
"a\\b\\c"
Is there any way in Java to make this easier?
Another way may be use equvivalent code for the symbols you want to escape.
Not really. I have made the transition not long ago and at first was constantly looking for "what is C#'s equivalent in Java for xyz?"
This is sometimes helpful but mostly frustrating. C# is a much more advanced language than Java and it will take a long time for Java to catch up.
You get used to it over time :-)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java “?” Operator for checking null - What is it? (Not Ternary!)
Java Null-pointer-safe accessor
Recently I read in one of the java forums about ?. operator. They wrote that ?. could not make it to the java 7. Can anybody explain what exactly ?. is?
Also, I like to know if this operator has any specific name or not like ?: is known as ternary operator.
It's called the Null-safe operator.
I believe you mean the Null-safe operator, explained here. It was under consideration for Java 7, but subsequently dropped.
The other common use for the ? in Java is the ternary operator, which has been in Java since the dark ages and is explained here.
The two are completely different features however, the only common element is that they use the ? in some way.