10 Replies - 77173 Views - Last Post: 18 August 2009 - 04:00 PM Rate Topic: -----

#1 csgwms  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-August 08

not equal to operator

Post icon  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? :blink:
Is This A Good Question/Topic? 0
  • +

Replies To: not equal to operator

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

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.
Was This Post Helpful? 1

#3 nick2price  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

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.
Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

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.

This post has been edited by Gloin: 18 September 2008 - 03:35 PM

Was This Post Helpful? 0
  • +
  • -

#5 csgwms  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-August 08

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
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: not equal to operator

Posted 18 September 2008 - 03:44 PM

I use them for Strings with no problems.
	if (eveType != null) 
			{
				JOptionPane.showMessageDialog(null, "Please select an Event");  
			}


where eveType is a String.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: not equal to operator

Posted 18 September 2008 - 03:50 PM

View Postnick2price, on 18 Sep, 2008 - 03:44 PM, said:

I use them for Strings with no problems.
	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



Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

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.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: not equal to operator

Posted 18 September 2008 - 04:45 PM

View Postnick2price, 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
Was This Post Helpful? 0
  • +
  • -

#10 bhawanapuri  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-April 09

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
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: not equal to operator

Posted 18 August 2009 - 04:00 PM

View PostGloin, on 18 Sep, 2008 - 02:30 PM, said:

You can also use goto statements in java although it's probably very uncommon.

Surely uncommon :D
goto is a reserver word in Java... in case they need to implement it later... but as of 1.6 not implement yet so won't even compile
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1