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 7 of 7

Thread: Just can't see why this loop is not working.

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Just can't see why this loop is not working.

    I am rather new to programming, I have done for loops before, but I can't see why this doesn't work.



    The following program is a fare calculator which I have been practicing on, I would really appreciate if someone could explain why this loop is printing "Add another drop-off point?" then breaking the loop after that and proceeding too the next section of code.



    for (int i = 0; i < 4; i++)
          {
             System.out.print("\nAdd another drop-off point? ");
     
             mulDropOff = scan.nextLine().toLowerCase().trim();
     
             if (mulDropOff.equals("yes") || mulDropOff.equals("y"))
             {
                System.out.print("\nEnter drop off point: ");
                nextDropOff = scan.nextLine().toLowerCase().trim();
                addDropOff = addDropOff + nextDropOff;
                sur = sur + 5.0;
                System.out.print("\nEnter extra distance (in km): ");
                addDist = scan.nextDouble();
                dist = dist + addDist;
     
             }
             else
             {   
                break;
             }
          }
    Last edited by Umbrafalx; September 11th, 2012 at 11:28 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Just can't see why this loop is not working.

    Because mulDropOff does not equals "yes" or "y". Print out the result of mulDropOff. I suspect scan.nextLine() is getting extra text that you are not expecting.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Just can't see why this loop is not working.

    Quote Originally Posted by aussiemcgr View Post
    Because mulDropOff does not equals "yes" or "y". Print out the result of mulDropOff. I suspect scan.nextLine() is getting extra text that you are not expecting.

    If I print mulDropOff (after setting it to equal "yes" at initialization) it will do that same thing, after printing out "yes" at the end of the previous string.

    String mulDropOff = "yes";
    for (int i = 0; i < 4; i++)
          {
          System.out.print("\nAdd another drop-off point? ");
          System.out.println(mulDropOff); 
          mulDropOff = scan.nextLine().toLowerCase().trim();

    Have I misinterpreted your suggestions?
    Also thankyou for not just giving me an answer in code, makes learning much better

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Just can't see why this loop is not working.

    for (int i = 0; i < 4; i++) {
    /*DEBUG*/System.out.println("In for loop, iteration number:" + i);
             System.out.print("\nAdd another drop-off point? ");
     
             mulDropOff = scan.nextLine().toLowerCase().trim();
    /*DEBUG*/System.out.println("   mulDropOff = " + mulDropOff);
             if (mulDropOff.equals("yes") || mulDropOff.equals("y"))
             {
    /*DEBUG*/System.out.println("      if statement was true.");
                System.out.print("\nEnter drop off point: ");
                nextDropOff = scan.nextLine().toLowerCase().trim();
                addDropOff = addDropOff + nextDropOff;
                sur = sur + 5.0;
                System.out.print("\nEnter extra distance (in km): ");
                addDist = scan.nextDouble();
                dist = dist + addDist;
     
             }
             else
             {   
    /*DEBUG*/System.out.println("      if statement was false");
                break;
             }
          }
    Try some printlns something like what I have added to your code sample above and see if you can determine why things are not going your way. You can add or remove printlns as you troubleshoot to narrow down where the problem is.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Just can't see why this loop is not working.

    Quote Originally Posted by Umbrafalx View Post
    If I print mulDropOff (after setting it to equal "yes" at initialization) it will do that same thing, after printing out "yes" at the end of the previous string.

    String mulDropOff = "yes";
    for (int i = 0; i < 4; i++)
          {
          System.out.print("\nAdd another drop-off point? ");
          System.out.println(mulDropOff); 
          mulDropOff = scan.nextLine().toLowerCase().trim();

    Have I misinterpreted your suggestions?
    Also thankyou for not just giving me an answer in code, makes learning much better
    See the recopy of your code below with comments.


    String mulDropOff = "yes";//setting mulDropOff to "yes"
    for (int i = 0; i < 4; i++)
          {
          System.out.print("\nAdd another drop-off point? ");
          System.out.println(mulDropOff); 
          mulDropOff = scan.nextLine().toLowerCase().trim();//setting mulDropOff to some value unknown to us before the test in the if statement yet to come.....
    To elaborate on the comments, You are setting the variable to a known, good value, but then that good value is being overwritten by some other value before you enter the if block's test. So you have not really done anything to change the flow of the program other than adding one step that doesn't count. See the code in my post #4 to try to correct the problem.
    Last edited by jps; September 12th, 2012 at 12:55 AM.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Just can't see why this loop is not working.

    Thank you guys, the issue is resolved. However, I am not exactly sure what the issue was.

    for (int i = 0; i < 4; i++)
          {
             mulDropOff = scan.nextLine();                              // Additional scan.nextLine()
             System.out.print("\nAdd another drop-off point? ");
             mulDropOff = scan.nextLine().toLowerCase().trim();
             if (mulDropOff.equals("yes") || mulDropOff.equals("y"))
             {
                System.out.print("\nEnter drop off point: ");
                nextDropOff = scan.nextLine().toLowerCase().trim();
                addDropOff = addDropOff + nextDropOff;
                sur = sur + 5.0;
                System.out.print("\nEnter extra distance (in km): ");
                addDist = scan.nextDouble();
                dist = dist + addDist;
     
             }
             else
             {   
                break;
             }
          }

    Can you give me an explanation? Was my program changing mulDropOff to the "scan" string and moving on before taking the next line?

    Also thank you for the debug output suggestion, that will be useful when debugging future code.
    Last edited by Umbrafalx; September 12th, 2012 at 02:27 AM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Just can't see why this loop is not working.

    The Scanner class holds what is read from the console in a buffer. That includes the line end character.
    The nextDouble() method does not read the line end char and leaves it in the buffer. The next call to nextLine() reads the line end from the buffer. To see that, print out the value of mulDropOff after it is read: S.o.p("mulDropOff=" + mulDropOff +"<");
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Loop not working
    By Lurie1 in forum Loops & Control Statements
    Replies: 12
    Last Post: March 7th, 2012, 06:27 AM
  2. Can someone please tell me why my do while loop isn't working?
    By JavaAsh in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 20th, 2011, 03:52 PM
  3. Loop conditions not working
    By bmxershane in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 1st, 2011, 02:25 AM
  4. [SOLVED] Loop Breaking Not Working
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 25th, 2010, 09:06 PM
  5. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 10:05 AM