Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: While Loop Exit with String

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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)


    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
    Last edited by helloworld922; November 8th, 2009 at 10:57 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: While Loop Exit with String

    ri=uleof thumb: Dont us != or == To compare strings. They are meant for numbers

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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...

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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: beaten to it

  8. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: While Loop Exit with String

    and i've notice in his program when i removed this

    licenseNumber = input.nextLine();

    it works fine when using the operator != or ==.

    hmmm ...

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    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)

    String s1 = new String("Test");
    String s2 = new String("Test");
     
    if (s1 != s2)
    {
        System.out.println("s1 and s2 have different references");
    }

Similar Threads

  1. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  2. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  3. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM
  4. JAVA for loop
    By tazjaime in forum Loops & Control Statements
    Replies: 2
    Last Post: August 18th, 2009, 07:43 PM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM