Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Tolls

Thread: While loop issue , the condition never occures

  1. #1
    Pirate1 is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default While loop issue , the condition never occures

    --- Solved -----

    The problem here , if i type "stop" , it just goes on and doesn't break the loop ...how to fix it ?
    Java Code:
    public static void test() 
        {
            //Instance object that will get the value from the user
            Scanner input = new Scanner(System.in);
            String fruitName = "";
            int priceKG = 0;
            
            System.out.println("Please type fruit's name , price per kilogram(in one line) : ");
            do 
            {            
                fruitName =input.next();            
                priceKG=input.nextInt();
            }while ("stop" != fruitName.trim());        
            System.out.println("Fruit name: " + fruitName);
            System.out.println("KG: " + priceKG);
        }
    thanks
    Last edited by Pirate1; 10-31-2013 at 01:29 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: While loop issue , the condition never occures

    When comparing Strings you need to use the equals() method, not == (or !=).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Pirate1 is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: While loop issue , the condition never occures

    I tried this but didn't work
    while (!fruitName.equals("stop"));

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

    Default Re: While loop issue , the condition never occures

    Works for me. Did you also include a price along with 'stop'?
    Of course, it doesn't do anything with the previous entries.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: While loop issue , the condition never occures

    Is it still looping, or is something else wrong?
    (Hint, "it doesn't work" gives us little information).

    I notice that the earlier one was using trim(), but this new one doesn't.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Pirate1 is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: While loop issue , the condition never occures

    I know it works but i have to enter "stop 0" or any digit ...all i want is to type "stop" without any number after, then the loop exits ..that' it.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: While loop issue , the condition never occures

    Then you need to check it before asking the user for a price.
    Possibly breaking out of the loop, rather than doing the check in the while.
    Pirate1 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: While loop issue , the condition never occures

    Quote Originally Posted by Pirate1 View Post
    I know it works but i have to enter "stop 0" or any digit ...all i want is to type "stop" without any number after, then the loop exits ..that' it.
    In other words: if the name equals "stop" your code shouldn't ask for a price (and quit the loop, as Tolls ninja'd on me).
    Last edited by gimbal2; 10-31-2013 at 01:25 PM. Reason: ninja
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Pirate1 is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: While loop issue , the condition never occures

    Quote Originally Posted by Tolls View Post
    Then you need to check it before asking the user for a price.
    Possibly breaking out of the loop, rather than doing the check in the while.
    You're right...fixed now ...thanks a lot man.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: While loop issue , the condition never occures

    Ninjas and pirates!

    Well it is/was halloween.

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: While loop issue , the condition never occures

    Is there a movie about ninja pirates? I don't think there is. I'd like to see one actually.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. While loop issue.
    By kylekep8 in forum New To Java
    Replies: 2
    Last Post: 10-05-2012, 08:58 AM
  2. ternary in the condition of a for loop
    By mikehaas763 in forum New To Java
    Replies: 0
    Last Post: 05-08-2012, 07:35 PM
  3. issue with loop or IDE
    By neoginn in forum New To Java
    Replies: 3
    Last Post: 10-28-2011, 06:59 AM
  4. Loop Issue
    By burrish in forum New To Java
    Replies: 11
    Last Post: 09-25-2011, 09:26 PM
  5. While loop condition
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 01:14 AM

Tags for this Thread

Posting Permissions

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