Reputation:
Please help to know how to compare two elements of a string in java. I can do it in C/C++ simply by string[i] == string[j]
this way. But my ide is showing error when I compare two elements of a string in java like c/c++.
Code:
String username = scanner.nextLine();
scanner.close();
int count = 0;
for (int i = 0; i < username.length() - 1; i++)
{
for (int j = i + 1; j < username.length(); j++)
{
if (username[i] == username[j]) // Error is here
{
count++;
break;
}
}
}
Thank you.
Upvotes: 1
Views: 1444
Reputation: 21
For reference data types like String, ==
compares the reference addresses of two objects, that is, whether the memory addresses are the same. If the memory addresses are the same, it is naturally the same object. What is the comparison between the same objects.
Our general application scenario is mainly to compare the contents of two String objects, then we need to use the equals()
method. We can look at the definition of equals()
method in java.lang.String
, we can see that equals()
is comparing the values of two String objects
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public Boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
There is also a special case, such as "abcde" == "abcde"
or "abcde" == "abc" + "de"
will both return true, because both are directly implemented by the compiler and have not been Declared as a variable.
Of course, if you know what you are doing, you have to take advantage of this feature of ==
, naturally there is no problem. At other times, use equals()
method.
Upvotes: 1
Reputation: 2511
In Java strings have their dedicated type String which is not an Array, so you can't access its characters with squared brackets. Instead, you should use String methods to compare 2 strings or access their characters. For string comparison use equals():
aString.equals(anotherString)
To access string's characters use charAt(...):
"Hello!".charAt(1); // returns 'e'
Upvotes: 1
Reputation: 104
If you want to compare 2 characters of String in java, for accessing those characters you have to use String function charAt(index)
.
For the above problem solution goes like this:
username.charAt(i) == username.charAt(j)
Upvotes: 1
Reputation: 121
string.charAt(i) == string.charAt(j)
Strings are objects not arrays
Upvotes: 1