Results 1 to 3 of 3
  1. #1
    ral1239 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default why does the body for my while loop always execute?

    Basically I'm looping through a txt file. Im using StringTokenizer to grab each string seperated by spaces on a line.
    On the first loop birthCity is Cleveland. tempStr becomes the next token, which is "77.8". tempChar becomes '7'. It goes back to the condition of the loop but still executes the body even when tempChar = '7'. It's something simple, but I'm seriously lost at the moment

    Java Code:
    while(tempChar != '0' || tempChar != '1'|| tempChar != '2' || tempChar != '3' || tempChar != '4' || tempChar != '5' || tempChar != '6' || tempChar != '7' || tempChar != '8' || tempChar != '9'){
    
                                birthCity = birthCity.concat(tokens.nextToken().concat(" "));
                                tempStr = tokens.nextToken();
                                tempChar = tempStr.charAt(0);
    when I do this it works, but I need all those conditions in the above code. Any suggestions?

    Java Code:
    Any suggestions?
    while(tempChar != '7'){
    
                                birthCity = birthCity.concat(tokens.nextToken().concat(" "));
                                tempStr = tokens.nextToken();
                                tempChar = tempStr.charAt(0);

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: why does the body for my while loop always execute?

    The first bit of code won't compile (look at the end of the while condition), but if you fixed it, think about what || does here. The while condition will be true if *any* of the subconditions are true. So just (tempChar != '0' || tempChar != '1') will always be true since tempChar can't equal both values meaning one or both of these conditions must *always* be true. Perhaps you want to use &&?

  3. #3
    ral1239 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: why does the body for my while loop always execute?

    Ah, my mistake. I should use && instead of ||. Thank you

Similar Threads

  1. hello every body
    By mark2 in forum New To Java
    Replies: 1
    Last Post: 09-13-2011, 12:22 AM
  2. How do I get my while loop to execute?
    By _inase in forum New To Java
    Replies: 2
    Last Post: 02-28-2011, 02:29 AM
  3. Hi..every body
    By hamudi in forum Introductions
    Replies: 2
    Last Post: 09-07-2008, 04:38 PM
  4. Can any body help me ?
    By Shyam Singh in forum New To Java
    Replies: 2
    Last Post: 08-22-2008, 12:04 AM
  5. Hello Every Body
    By Hassan Hafez in forum Introductions
    Replies: 2
    Last Post: 06-29-2008, 05:40 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
  •