While Loop Exit with String
This program works, although I don't understand how. I used }while ( !licenseNumber.equalsIgnoreCase ("quit") ); to exit the program when the user types quit but why doesn't it work when I write it like this licenseNumber != "quit"; doesn't it mean the samething basicly(I know the second one is case sensitive)
Code :
import java.util.Scanner;
public class TrafficFines
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String licenseNumber = "quit";
System.out.println("Welcome to the Traffic Fine Reporting System.\n" +
"Please enter the requested data for each fine.");
do
{
System.out.println("Enter the drivers license number - 'quit' to exit:");
licenseNumber = input.nextLine();
}while ( !licenseNumber.equalsIgnoreCase ("quit") );
}// end method main
}// end class ConvertTemperature2
Re: While Loop Exit with String
String is an object, so checking equality with == or != is checking whether they are the same reference, which may or many not be the case for identical strings. You should always use the equals (or alternatively equalsIgnoreCase for Strings) when checking for Object equality. Visit the following thread for another description and example:
http://www.javaprogrammingforums.com...ing-false.html
Re: While Loop Exit with String
ri=uleof thumb: Dont us != or == To compare strings. They are meant for numbers
Re: While Loop Exit with String
Not true, != and == are very useful for comparing if two objects are the same object, not if they contain the same data.
Re: While Loop Exit with String
is it because of referencing? is it because Strings are not primitive data type? is it because its an object?
any brief explanation regarding with this? im also curious with that matter...
Re: While Loop Exit with String
When using == and != on non objects like primitives its compares the values, however when using the same way to compare two objects you will be comparing the actual object so see if they have the same handler id more or less in the native back end of things. Strings are somewhat tricky because they will compare using == sometimes because of the way Java handles strings.
The general rule is that if you wish to compare two objects to see if they are meaningfully equals, that is they have the same values you should use the equals method. When creating your own classes you can of course override the equals method and implement your own check.
// Json
Re: While Loop Exit with String
Yes, that's exactly why.
Say we had the two variables:
Object A and Object B.
The value contained by A and B are addresses to where some objects are.
If A==B, then they are pointing to the same object (can you see why?)
However, if A and B pointed to two different objects, A != B, no matter what data is actually contained in the two objects.
edit: :P beaten to it
Re: While Loop Exit with String
and i've notice in his program when i removed this
Code :
licenseNumber = input.nextLine();
it works fine when using the operator != or ==.
hmmm ...
Re: While Loop Exit with String
That's one of the quirky ways java optimizes strings. Instead of creating multiple constant strings, sometimes the Java compiler will recognize that it already has that object and just assigns the same reference:
Code :
String s1 = "Test";
String s2 = "Test";
if (s1 == s2)
{
System.out.println("s1 and s2 have the same reference!");
}
However, you can explicitly tell Java to create a new variable (and actually, the code above will fail sometimes because that optimization isn't used all the time)
Code :
String s1 = new String("Test");
String s2 = new String("Test");
if (s1 != s2)
{
System.out.println("s1 and s2 have different references");
}