1. Member
Join Date
Feb 2013
Posts
7
Rep Power
0

## Do loop help

I'm in need of a little help... I am trying to use a do loop and I'm stuck on how to make the second inner loop to when it prints "Calculate another interest?" if they type yes to loop up and ask the loan amt, interest, and term again? Any help would be great thanks!

Java Code:
```public static void main(String[] args )
{
Scanner input = new Scanner(System.in);

do{
//Prompt user to enter loan amount
System.out.print("Enter Loan Amount: ");
int principle = input.nextInt();

//Prompt user to enter interest rate
System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
double interest = input.nextDouble();

//Prompt user to enter term
System.out.print("Enter the Term (in months): ");
int term = input.nextInt();

if (principle > 0)
{
if (interest >= 0 && interest <= 100)
{
if(term > 0)
{
double simple= principle*(interest/100.0)*(term/12.0);
double repaidSimple= simple+principle;
double a = 1.0+((interest/100.0)/365.0);
double b = (365.0*(term/12.0));
double daily = Math.pow(a,b);
double dailyCompound = (principle*daily)-principle;
double repaidDaily = dailyCompound + principle;
double c = 1.0+((interest/100)/12.0);
double d = (12.0*(term/12.0));
double monthly = Math.pow(c, d);
double monthlyCompound = (principle*monthly)-principle;
double repaidMonthly = monthlyCompound + principle;
int type = 0;

//Prompt user to enter calculation type
do{System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):");
type = input.nextInt();

switch(type)
{
case 1:System.out.println("Total calculated interest: "  +simple);
System.out.println("Total amount to be repaid: "  +repaidSimple);
break;
case 2:System.out.println("Total calculated interest: "  +monthlyCompound);
System.out.println("Total amount to be repaid: "  +repaidMonthly);
break;
case 3:System.out.println("Total calculated interest: "  +dailyCompound);
System.out.println("Total amount to be repaid: "  +repaidDaily);
break;
default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type);
break;

}
}while(type<1||type>3);
}
else
{
System.out.println("Data Error: Term must be greater than zero. You entered " +term);

}
}
else
{
System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +interest);
}
}
else
{
System.out.println("Data Error: Loan amount must be greater than zero. You entered " +principle);
}

do{
System.out.println("Calculate another loan interest (Yes/No)?");
}while(input.next().equalsIgnoreCase("yes")&& input.next().equalsIgnoreCase("no"));

}while (input.next().equalsIgnoreCase("yes"));
System.out.println("Thank you for using Interest Calculator!");
}
}```

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,926
Rep Power
10

## Re: Do loop help

First, your loops won't ever work properly as written:
Java Code:
``` do{
System.out.println("Calculate another loan interest (Yes/No)?");
}while(input.next().equalsIgnoreCase("yes")&& input.next().equalsIgnoreCase("no"));```
Two problems here:

So you should normally do this:

Java Code:
```String answer = input.next();
do{
System.out.println("Calculate another loan interest (Yes/No)?");
There is still a huge problem. Answer can't be both yes and no at the same time so the loop will always
fail.

Try rewriting the code and putting in print statements as debugging aids.

Regards,
Jim

3. Member
Join Date
Feb 2013
Posts
7
Rep Power
0

## Re: Do loop help

Java Code:
```do{
System.out.println("Calculate another loan interest (Yes/No)?");
}while(input.next().equalsIgnoreCase("yes"));```
I see that this will cause a problem, I'm just having trouble where I should place the
Java Code:
` do{`
and then where I should place the
Java Code:
` }while(input.next().equalsIgnoreCase("yes"));`
to make the program ask for the principle, interest, and term again..
Thanks for the quick response, I'm still new to using java so bear with me!

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,926
Rep Power
10

## Re: Do loop help

Your big loop which does all the processing should be the inner loop. The outer loop is the one which asks if the user wants to repeat the process. So in essence, the outer loop and question/answer determines if the inner loop will run.

Regards,
Jim

5. Member
Join Date
Feb 2013
Posts
7
Rep Power
0

## Re: Do loop help

I think I have it, well it seems to be working properly for me... now when I need is when it prompts "Calculate another loan interest (Yes/No)?" if you type in Okay, maybe, basically anything but yes, no, y,n, etc it returns the error "Please enter 'Yes' or 'No'" Am I just setting it up wrong or how should I key that in? use an if statement?

Java Code:
``` public static void main(String[] args )
{
Scanner input = new Scanner(System.in);

do{

do{

//Prompt user to enter loan amount
System.out.print("Enter Loan Amount: ");
int principle = input.nextInt();

//Prompt user to enter interest rate
System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
double interest = input.nextDouble();

//Prompt user to enter term
System.out.print("Enter the Term (in months): ");
int term = input.nextInt();

if (principle > 0)
{
if (interest >= 0 && interest <= 100)
{
if(term > 0)
{
double simple= principle*(interest/100.0)*(term/12.0);
double repaidSimple= simple+principle;
double a = 1.0+((interest/100.0)/365.0);
double b = (365.0*(term/12.0));
double daily = Math.pow(a,b);
double dailyCompound = (principle*daily)-principle;
double repaidDaily = dailyCompound + principle;
double c = 1.0+((interest/100)/12.0);
double d = (12.0*(term/12.0));
double monthly = Math.pow(c, d);
double monthlyCompound = (principle*monthly)-principle;
double repaidMonthly = monthlyCompound + principle;
int type = 0;

//Prompt user to enter calculation type
do{System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):");
type = input.nextInt();

switch(type)
{
case 1:System.out.println("Total calculated interest: "  +simple);
System.out.println("Total amount to be repaid: "  +repaidSimple);
break;
case 2:System.out.println("Total calculated interest: "  +monthlyCompound);
System.out.println("Total amount to be repaid: "  +repaidMonthly);
break;
case 3:System.out.println("Total calculated interest: "  +dailyCompound);
System.out.println("Total amount to be repaid: "  +repaidDaily);
break;
default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type);
break;

}
}while(type<1||type>3);
}
else
{
System.out.println("Data Error: Term must be greater than zero. You entered " +term);

}
}
else
{
System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +interest);
}
}
else
{
System.out.println("Data Error: Loan amount must be greater than zero. You entered " +principle);
}

System.out.println("Calculate another loan interest (Yes/No)?");
}while(input.next().equalsIgnoreCase("yes"));
System.out.println("Thank you for using Interest Calculator!");

}while (input.next().equalsIgnoreCase("yes"));

}
}```

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,926
Rep Power
10

## Re: Do loop help

Here is skeleton of what I think you want to do.

Jim

Java Code:
```boolean val = true;
while (val) {
System.out.print("Would you like to play a game?");
String ans = input.next();
if (ans.equal("yes")) {
do {

// Your processing code goes here.

} while ( /* Some inner test here */ );
} else {
val = false;  //causes outer loop to fall thru  (you could also use a break here)
}
}

System.out.println("Thank you for playing");```

#### Posting Permissions

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