This question already has answers here:
Why should a Java class implement comparable?
(10 answers)
Closed 6 years ago.
String ntext;
ntext = something;
String currentLine;
currentLine = something;
while(ntext.compareTo(currentLine) != 0){
//some condition
}
Here i want to know what that compareto actually do.
One more questin what we can use to compare two objects?
If those variables are strings (i assume so) it checks if they are equal, returns 0 if so, and another number if not. See the JavaDoc here: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo%28java.lang.String%29
More specifically, it goes through both strings character by character. When it finds a string of one that is not equal to the other, it returns a number representing whether the differing character is more than, less than, or equal to the corresponding character in the other string.
Related
This question already has answers here:
split string at index
(5 answers)
Closed 2 years ago.
I get a number generated as a string that always has the same length, for example:
0107612733631449211907028445
Now I wan't to get two separate strings with the last 10 digits:
010761273363144921**1907028445**
And another string with these digits
0107612733**63144**9211907028445
The positions of these needed numbers are always the same, the numbers at the beginning are not important for me and can be omitted.
How can I get these two strings?
Java docs .substring method
String number = "0107612733631449211907028445";
String last10 = number.substring(number.length()-10);
for middle numbers use substring(int beginIndex, int endIndex)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
I am encountering a weird problem. I was trying to compare two 128 bits strings and I believe every char of them match each other(and I tested comparing String.chartAt(#) several times) but when I do
if (String 1== String 2)
..
else
..
It went to else clause. Why is that?
When using == for comparison, you are checking if both variables refer to the same object (reference comparision). You should use strings equals() method in order to compare if they are equal in a sense they consist of same characters.
description of equals method in java documentation
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if
and only if the argument is not null and is a String object that
represents the same sequence of characters as this object.
This question already has answers here:
Comparing strings with == which are declared final in Java
(6 answers)
Closed 9 years ago.
while comparing to strings we can do using == or .equals()
In == we know that it checks for references but in .equals() it checks for contents.
So suppose if there are 2 strings say
String s="SO";
String s1="SO";
so in this case s1==s and s.equals(s1) both will give true.
But here it gives me false
So what I assume is + is high priority than ==
so in this case
System.out.println(""+s1==s);
it will be splitted like (""+s1)==s and now ""+s1 will be a new String and hence the new String will never be equal to s so its printing false
I am just interested to know whether I thought is right or not
""+s1 creates a new String Object on the heap (since it is not declared as final). So, the references will not be same.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
i am trying to do a presence check in java on 10 JTextFields. I want it so that if all 10 of my textfield have something in them, it will do my code.
String input1 = tfQ1.getText();
String input2 = tfQ2.getText();
etc.
I have put
IF(input1==("")&&input2==("")&&input3==("")&&input4==("")&&input5==("")&&input6==("")&&input7==("")&&input8==("")&&input9==("")&&input10==(""))
{
//DO SCORES ETC
}
However, this doesnt do anything... (my button does not work weather there are things in the text fields or not)
Please and someone help with presence check validation? Thanks =)
instead of "==" operator you should use
input1.equals("")
if(input.equals("WhateverYouAreLookingFor")) {
//do this
}else {
//do this
}
== is a reference comparison, both objects point to the same location in memory. essentially it tests wether the two operands refer to the same object.
.equals() will only compare what is in the String. It can be overridden so two distinct objects can still be equal.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am getting the two different path that are basically file path , injected through xml by spring ioc..
private String FilePath1; //it consists path c:\abc folder
private String FilePath2; //it consists of c:\abc\def
now I need to check that if they two are equal then they should go inside the
condition otherwise not
please advise how to achieve this..
what I have tried is ..
if (FilePath1 =! FilePath2)
{
filemove(mcrpFilePath, zipfileName); //i want if two file paths are not wqula then it // should go inside filemove
}
Use equals() to compare Java Strings. The reason for this is because Strings are objects in Java. Therefore, the == operator tests to see if two objects point to the same instance! not whether they are equal to each other.