-2

I am trying to prompt the user if the information they entered is correct and the program just proceeds anyway instead of re-prompting the user. I think i need to change how I loop this do some type of do-while loop, but I don't quite know what to do.

{

    //Here we will set up our method of data entry and decimal format
    Scanner keyboard = new Scanner(System.in);
    DecimalFormat eNotation1 = new DecimalFormat("00.00");

    //Let's introduce our program to the user
    System.out.println("Hi, This is a program to calculate quarterly interest growth");

    //Here we will prompt the user to enter the amount of quarters
    System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
    int quarterNumber = keyboard.nextInt();
    if (quarterNumber > 10 || quarterNumber < 0)
    {
    System.out.println("You entered a number outside of the boundrys. Please run the program again and choose a number between 0 and 10 (number can include 10)");
    System.exit(0);
    }

    //Here we will prompt the user for the starting balance.
    System.out.println("What is the starting balance of this account?");
    double startingBalance = keyboard.nextDouble();

    //Here we will prompt the user for the interest rate
    System.out.println("What is the interest rate (in percent) in your area? Please pick a number from 1 - 20.");
    double interestRate = keyboard.nextDouble();
    if (interestRate < 1 || interestRate > 20)
    {
    System.out.println("You entered a number outside of the boundrys. Please run the program again and choose a number between 1 and 20 (can include 1 and 20)");
    System.exit(0);
    }

    //Here we will double check with the user that the info is correct.
    System.out.println("The amount of quarters you would like displayed is " + quarterNumber);
    System.out.println("The starting balance you would like to work with is " + startingBalance);
    System.out.println("The interest rate in your area is " + interestRate);
    System.out.println("Is this information correct?");
    keyboard.nextLine();
    String answer = keyboard.nextLine();
    System.out.println("");

    //We will have them enter the information again if the answer is no or No
    if (answer == "no" || answer == "No")
    {
    //Here we will prompt the user to enter the amount of quarters
    System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
    keyboard.nextInt();
    quarterNumber = keyboard.nextInt();

    //Here we will prompt the user for the starting balance.
    System.out.println("What is the starting balance of this account?");
    keyboard.nextDouble();
    startingBalance = keyboard.nextDouble();

    //Here we will prompt the user for the interest rate
    System.out.println("What is the interest rate (in percent) in your area? Please pick a number from 1 - 20.");
    keyboard.nextDouble();
    interestRate = keyboard.nextDouble();
    }

    //Next we will proceed with the calculation if the information is indeed correct
    double endingBalance = startingBalance + (startingBalance * (interestRate / 100 * .25));
    double interestEarned = endingBalance - startingBalance;

    //Now we will output the information
    for (int qn = 1; qn < (quarterNumber + 1); qn++ , startingBalance = startingBalance + interestEarned , endingBalance =startingBalance + (startingBalance * (interestRate / 100 * .25)), interestEarned = endingBalance - startingBalance )
    {
    System.out.println("Quarter Number                      Starting Balance                      Interest Earned                           Ending Balance   ");
    System.out.println(qn + "                                   " + eNotation1.format(startingBalance) + "                                " + eNotation1.format(interestEarned) + "                                      " + eNotation1.format(endingBalance) + "   ");                
    }   


 }

}

2
  • You have to tell us where exactly your error is. Also, you didn't tell us what keyboard represents. Also, simplify your problem into a new project or something so it is easier to understand your problem.
    – aeee98
    Commented Mar 1, 2016 at 2:35
  • Ok sure, this is my first semester of programming as you can probably tell, not used to asking these questions. Here is more detail, I first prompt the user to enter the 3 variables: 'quarterNumber' , 'startingBalance', and 'interest rate'. I then try to ask them if the information is correct. I'd like the questioning to start over if the user inputs: "no" or "No". Currently when i run the program i type "no" and it does not prompt me for input again as designed.
    – Kevin
    Commented Mar 1, 2016 at 2:41

3 Answers 3

0

I probably figured what your problem is.

Example code snippet: (Note I am doing without an actual java editor so just handle any missing syntax

 //Here we will prompt the user to enter the amount of quarters
int quarters = 0;
System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
do
{
quarters = keyboard.nextInt();
if (quarters <= 0 || quarters >= 10)
{
System.out.println("You entered a number outside of the boundrys. Please Type a number greater than 0 and less than equal to 10 again)");
}
}
while (quarters <= 0 || quarters >= 10)

The idea is for a recursive prompt. The application first prompts the user to type in the result, and then you handle the amount of So you have to handle the prompt within the do-while loop. Assuming you don't want to repeat the same old text, you can do what I did (which is much cleaner in my opinion).

EDIT: Assuming you want to handle the entire repeating of the application. place everything for the declaration of variables into a method (or better various methods for cleaner handling, and handle something like this.

int quarters;
double balance;
//TODO : Add all other required variables
string verification;

do
{
quarters = getNumberOfQuartersFromUser();
balance = getStartingBalance();
... //TODO : all the other methods
//verification
System.out.println("Is this accepted?");
verification = keyboard.nextString();
//Handle verfication if it is no or No
if (verification == null || verification == "No" || verification == "no")
{
System.out.println("Starting from the beginning again");
}
}
while (verification == null || verification == "No" || verification == "no")
//out of loop, so handle calculation.
...

and a method snippet

private int getNumberOfQuartersFromUser()
{
int quarters = 0;
System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
do
{
quarters = keyboard.nextInt();
if (quarters <= 0 || quarters >= 10)
{
System.out.println("You entered a number outside of the boundrys. Please Type a number greater than 0 and less than equal to 10 again)");
}
}
while (quarters <= 0 || quarters >= 10)
return quarters;
}
1
  • Yes this is perfect and so much better! Thank you so much sir!
    – Kevin
    Commented Mar 1, 2016 at 3:04
0

I think you should enclose your codes to Else statement to avoid proceeding unintentionally. Remember that else limits the run-time of your codes and put it on a more specified path for you.

5
  • Hi, thank you for helping. I'm not quite sure when I would put my Else statements. Would I say something like if answer == yes or Yes then continue. Else reprompt user for input?
    – Kevin
    Commented Mar 1, 2016 at 2:46
  • if (condition){ (action) } else{ (action) } Commented Mar 1, 2016 at 2:51
  • remember that you could perform a nested if statement to avoid confusion on the code. if your condition is met do this, if not put "else" for another action. and if you have another condition, you could put else if(condition){}. Commented Mar 1, 2016 at 2:55
  • So i tried doing this. My problem is that after my if statement evaluates to true - user enters value of 'no' or 'No" - the program does not re-prompt me to enter the variables as intended. I think because they are defined earlier in the program.
    – Kevin
    Commented Mar 1, 2016 at 2:58
  • ok I'll try to edit your code and post it as another answer for you to understand what I mean by nested if statement which is basically what you need to fix your problem since it is difficult to explain through comments Commented Mar 1, 2016 at 3:05
0
{

//Here we will set up our method of data entry and decimal format
Scanner keyboard = new Scanner(System.in);
DecimalFormat eNotation1 = new DecimalFormat("00.00");

//Let's introduce our program to the user
System.out.println("Hi, This is a program to calculate quarterly interest growth");

//Here we will prompt the user to enter the amount of quarters
System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
int quarterNumber = keyboard.nextInt();

//Here you had started your condition I'll try to make it nested if statement
if (quarterNumber > 10 || quarterNumber < 0)
//if the condition is true perform this
{
System.out.println("You entered a number outside of the boundrys. Please run the program again and choose a number between 0 and 10 (number can include 10)");
System.exit(0);
}
else{
//if false conditions inside else statement should be performed

//Here we will prompt the user for the starting balance.
System.out.println("What is the starting balance of this account?");
double startingBalance = keyboard.nextDouble();

//Here we will prompt the user for the interest rate
System.out.println("What is the interest rate (in percent) in your area? Please pick a number from 1 - 20.");
double interestRate = keyboard.nextDouble();

//you could put another if statement inside if statement which is known as nested if statement.
if (interestRate < 1 || interestRate > 20)
{
System.out.println("You entered a number outside of the boundrys. Please run the program again and choose a number between 1 and 20 (can include 1 and 20)");
System.exit(0);
}
else{
//like before you should enclose this to else to avoid confusion

//Here we will double check with the user that the info is correct.
System.out.println("The amount of quarters you would like displayed is " + quarterNumber);
System.out.println("The starting balance you would like to work with is " + startingBalance);
System.out.println("The interest rate in your area is " + interestRate);
System.out.println("Is this information correct?");
keyboard.nextLine();
String answer = keyboard.nextLine();
System.out.println("");

//We will have them enter the information again if the answer is no or No
if (answer == "no" || answer == "No")
{
//Here we will prompt the user to enter the amount of quarters
System.out.println("How many quarters would you like to display? Please pick a number greater than zero and less than or equal to 10");
keyboard.nextInt();
quarterNumber = keyboard.nextInt();

//Here we will prompt the user for the starting balance.
System.out.println("What is the starting balance of this account?");
keyboard.nextDouble();
startingBalance = keyboard.nextDouble();

//Here we will prompt the user for the interest rate
System.out.println("What is the interest rate (in percent) in your area? Please pick a number from 1 - 20.");
keyboard.nextDouble();
interestRate = keyboard.nextDouble();
}

//Next we will proceed with the calculation if the information is indeed correct
double endingBalance = startingBalance + (startingBalance * (interestRate / 100 * .25));
double interestEarned = endingBalance - startingBalance;

//Now we will output the information
for (int qn = 1; qn < (quarterNumber + 1); qn++ , startingBalance = startingBalance + interestEarned , endingBalance =startingBalance + (startingBalance * (interestRate / 100 * .25)), interestEarned = endingBalance - startingBalance )
{
System.out.println("Quarter Number                      Starting Balance                      Interest Earned                           Ending Balance   ");
System.out.println(qn + "                                   " + eNotation1.format(startingBalance) + "                                " + eNotation1.format(interestEarned) + "                                      " + eNotation1.format(endingBalance) + "   ");                
}   
}
}
}

Remember that learning nested if statement is essential to a programmer. without this, all your conditions will literally proceed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.