Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default While Loops and Strings

    So the while loop I am trying to use is:
    while( type != "EXIT" )
    {
    type = input.next();
    }


    the problem is that typing in EXIT doesn't end the loop like I want it to. I also tried input.nextLine(); but neither of them work. The loop is being used to fill an ArrayList so the number of elements can change based on how many they want. What am I doing wrong, or what alternatives do I have?

    Thanks!

    EDIT: nevermind I think I figured it out. seems I need to use !type.equals("EXIT")
    Last edited by d0ct0rscarecr0w; 01-03-2014 at 08:32 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,912
    Rep Power
    5

    Default Re: While Loops and Strings

    Don't use == or != when evaluating strings. Use equals().

    e.g.
    Java Code:
    while (!type.equals("EXIT"))
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: While Loops and Strings

    EDIT: nevermind I think I figured it out. seems I need to use !type.equals("EXIT") <--

    Last edited by d0ct0rscarecr0w; Today at 07:32 AM <--
    Sorry Jim, you're still only a software engineer and not a doctor ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,912
    Rep Power
    5

    Default Re: While Loops and Strings

    Geez. I must have totally glossed over it.

    Regards,
    jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. for-to and do-x loops
    By AndrewM16921 in forum Forum Lobby
    Replies: 0
    Last Post: 12-13-2012, 10:32 AM
  2. Need help with while loops
    By tangel in forum New To Java
    Replies: 0
    Last Post: 10-04-2011, 05:37 AM
  3. Question about sentinal loops with strings
    By dienesh77 in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 02:57 AM
  4. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  5. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-04-2009, 12:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •