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.
Code Java:
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;
}
}
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.
Re: Just can't see why this loop is not working.
Quote:
Originally Posted by
aussiemcgr
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.
Code Java:
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 :)
Re: Just can't see why this loop is not working.
Code Java:
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.
Re: Just can't see why this loop is not working.
Quote:
Originally Posted by
Umbrafalx
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.
Code Java:
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.
Code Java:
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.
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.
Code java:
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.
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 +"<");