not equal to operator
Page 1 of 110 Replies - 66127 Views - Last Post: 18 August 2009 - 04:00 PM
#1
not equal to operator
Posted 18 September 2008 - 03:17 PM
hi i was wonder how to say "is not equal to" using the java STRING operator not the other one "!=" and i was also wondering how i loop a certain part of a code, is there such thing as a loop command?
Replies To: not equal to operator
#2
Re: not equal to operator
Posted 18 September 2008 - 03:25 PM
The String class uses a method called equals(String str2) to compare one string to another.
The method returns true if the strings compared are equal and false otherwise.
To negate the result, just put an exclamationmark in front of the statement.
String str1 = "hi";
String str2 = "bye";
boolean notEqual = !str1.equals(str2);
would generate the value true to be put in the variable notEqual although the strings are not equal.
The method returns true if the strings compared are equal and false otherwise.
To negate the result, just put an exclamationmark in front of the statement.
String str1 = "hi";
String str2 = "bye";
boolean notEqual = !str1.equals(str2);
would generate the value true to be put in the variable notEqual although the strings are not equal.
#3
Re: not equal to operator
Posted 18 September 2008 - 03:25 PM
Why would you want to use the java String operator(if there is one) instead of "!="?
And there are many ways to loop code, look for while loops, for loops, do while loops, if statements etc
for loops is problably the most popular choice.
And there are many ways to loop code, look for while loops, for loops, do while loops, if statements etc
for loops is problably the most popular choice.
#4
Re: not equal to operator
Posted 18 September 2008 - 03:30 PM
There are several ways to use loops in java.
most common are for- or while-loops
ex:
for (int i = 0; i < 10; i++) {
(code to be looped here)
}
int = 0;
while (i < 10) {
(code to be looped here)
i++;
}
You can also use goto statements in java although it's probably very uncommon. Then there's the Iterator class among others.
while editing I can add that != isn't to be applied to strings that way. I believe you can use the == and != operators but you would be comparing the memoryaddresses or something instead of the contents of the variables.
most common are for- or while-loops
ex:
for (int i = 0; i < 10; i++) {
(code to be looped here)
}
int = 0;
while (i < 10) {
(code to be looped here)
i++;
}
You can also use goto statements in java although it's probably very uncommon. Then there's the Iterator class among others.
while editing I can add that != isn't to be applied to strings that way. I believe you can use the == and != operators but you would be comparing the memoryaddresses or something instead of the contents of the variables.
This post has been edited by Gloin: 18 September 2008 - 03:35 PM
#5
Re: not equal to operator
Posted 18 September 2008 - 03:31 PM
because the != doesnt work for strings? i thought it only worked with numbers
#6
Re: not equal to operator
Posted 18 September 2008 - 03:44 PM
I use them for Strings with no problems.
where eveType is a String.
if (eveType != null)
{
JOptionPane.showMessageDialog(null, "Please select an Event");
}
where eveType is a String.
#7
Re: not equal to operator
Posted 18 September 2008 - 03:50 PM
nick2price, on 18 Sep, 2008 - 03:44 PM, said:
I use them for Strings with no problems.
where eveType is a String.
if (eveType != null)
{
JOptionPane.showMessageDialog(null, "Please select an Event");
}
where eveType is a String.
You shouldn't
!!= and == for String means that they point to same object....
In sthe same class
{
String a = "abc";
String b = "abc";
a == b will be true because they both point to the constant "abc" and the compiler is smart enough to see that it is the same one but
a = "abc";
b = "a";
b += "b";
b += "c";
a == b wil be false
#8
Re: not equal to operator
Posted 18 September 2008 - 04:04 PM
I can see in your example why it would be bad to use != and == for comparing Strings. I generally use it to compare it to a null value which doesnt seem to cause any problems. But if this is bad, i shall start doing it the proper way.
#9
Re: not equal to operator
Posted 18 September 2008 - 04:45 PM
nick2price, on 18 Sep, 2008 - 04:04 PM, said:
I can see in your example why it would be bad to use != and == for comparing Strings. I generally use it to compare it to a null value which doesnt seem to cause any problems. But if this is bad, i shall start doing it the proper way.
Sure you can do:
if(str == null)
which means if the str object points to nothing
but if you receive a String from another method
String name = method.getName();
if name contains ""
if(name == "") whill be false because the constant "" in your class is not the same String object as the one in the other method
#10
Re: not equal to operator
Posted 18 August 2009 - 02:42 AM
//To remove a character
public class main3 {
public static void main(String args[]) {
String str = "this is a test";
System.out.println(removeChar(str,'a'));
}
public static String removeChar(String s, char c) {
String r = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i)!= c)
r += s.charAt(i);
}
return r;
}
}
// the output should be :thi i a tet but, does work
public class main3 {
public static void main(String args[]) {
String str = "this is a test";
System.out.println(removeChar(str,'a'));
}
public static String removeChar(String s, char c) {
String r = "";
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i)!= c)
r += s.charAt(i);
}
return r;
}
}
// the output should be :thi i a tet but, does work
#11
Re: not equal to operator
Posted 18 August 2009 - 04:00 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|