Hello,

I'm a bit new here, and also quite new to java. I'm wondering how I would insert a print statement into the do while loop, if the while is false.

do {
      System.out.println("Enter number of years for the loan an in integer:  ");
      numberOfYears = input.nextDouble();
  
      System.out.println("Enter the loan amount :  ");
      loanAmount = input.nextDouble();
      } while (loanAmount <= 0 || numberOfYears <= 0) ;

I want System.out.println("Your numbers must be positive, please try again.");
if the while is false right before the code gets re-executed.


Also, how would I fit a % inside the following statement, since it's using specifier, it won't let me put another % sign that I need because it's a percent value!

System.out.printf("   %.3f" ,annualInterestRate);

Thanks!

Edited 6 Years Ago by Cheese Man 808: n/a

You can just a have print message after the loop to tell the user your message, since the only way to break out of the loop is for a number to be negative. And then to make it continue you can do something like this:

boolean bEscape = true;
do{
    do {
      System.out.println("Enter number of years for the loan an in integer:  ");
      numberOfYears = input.nextDouble();
  
      System.out.println("Enter the loan amount :  ");
      loanAmount = input.nextDouble();
      } while (loanAmount <= 0 || numberOfYears <= 0) ;
  System.out.println("Your numbers must be positive, please try again.");
}while(bEscape != true)

For your other question I,m sorry but I don't use that type of coding.

Edited 6 Years Ago by Akill10: boolean bEscape = true; // not false! :)

To insert a % in a printf, you use another % to indicate it's a literal % character: %%
So your expression would look like " %%%.3f", aIR);


As for the print statement, if you put an

if (value is out of range) print (error message);

at the top of your loop. This works because do...while always executes the first time.

Edited 6 Years Ago by jon.kiparsky: n/a

To insert a % in a printf, you use a backslash-escape: \%
So your expression would look like " \%%.3f", aIR);

As for the print statement, if you put an

if (value is out of range) print (error message);

at the top of your loop. This works because do...while always executes the first time.

How would the if statement work though? The values wouldn't be recognised if they are below the if statement would they? i suppose that would work if you put it at the bottom of the loop. But it would still jump from the loop, which is why I encased it in another do-while :)

Edited 6 Years Ago by Akill10: n/a

You're right - I didn't really read the code, didn't see the input was in the loop.
Yeah, anywhere after the values are entered, it'll work.

In fact, you might as well make it an infinite loop, and put a break in the if, rather than doing the same check twice.

so:

while (math.pow(565,0)==1)  // or some other always-true condition...
{
  get input;
  if (input out of range)
     complain();     //complain: inform user of their mistake

  else
     break;
}

That's what you want, right? Repeat the loop until the user gives you good input, and complain each time they fail?

Or, you could do the inverse of this:

while (!done)  
{
  get input;
  if (input is in range)
     done==true;
  else 
     gripe();
}

There are numerous variations on this theme, eg

for (int i = 0; i<1; i=getUserInput(inputBlob));

where getUserInput takes an "InputBlob" containing two strings, and puts the input into them, returning a value >1 if input is successful.

(Please don't actually do this...)

Edited 6 Years Ago by jon.kiparsky: code tags

Thanks jon, that seemed to have solved the problem.

Akill10, I'm still trying to go through your code, but I did copy and paste it in just to test, and the code never becomes true to get through the loop (any value entered will be false in the end). I'm sure I just need to edit it, that was just a quick test.

hmmmm :)

Ohhhhh my bad....the outer while condition should be

while(bEscape == true)

and by the way, you have your first while loops conditions the wrong way around to do it my way. :)

Edited 6 Years Ago by Akill10: n/a

do {
      System.out.println("Enter number of years for the loan an in integer:  ");
      numberOfYears = input.nextDouble();
  
      System.out.println("Enter the loan amount :  ");
      loanAmount = input.nextDouble();
      } while (loanAmount <= 0 || numberOfYears <= 0) ;

I do not find it logical to get two user inputs in this context (by 'context' I mean: in the same loop, right after each other). Assume the user gives one correct input, and one wrong input. Then the following will happen: Both values will be discarded, and the user will be requested to re-enter values for both input requests. Personally I don't find it very user-friendly if a correct input gets discarded, just because another input request (from inside the same loop) got some invalid user-input. I suggest that each input request be put in its own loop. Different looping alternatives have already been suggested. Or even better: create input routines, one for reading the number of years, and one for reading the loan amount. Both routines should be written in such a way that they only return on valid input.

This article has been dead for over six months. Start a new discussion instead.