1

I have to make a calculator that will

  1. Run program with a positive number input, display output and ask if user wants to loop.

  2. Run program with string input (two instead of 2), use try and catch to display a message telling the user of the error, and ask if user wants to loop.

  3. Run program with negative number input, display a error message without displaying the output, and ask the user if they want to loop.

When I try catch I get variable not have been initialized for variables after the try catch.

try {
    System.out.print("Enter loan amount: ");
    loanNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter rate: ");
    rateNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter number years: ");
    yearNum = Double.parseDouble(in.nextLine());
}
catch(NumberFormatException e) { 
    System.out.println ("You must enter positive numeric data!");
}

Without the try catch I can't figure out how to do number 2 without using System.exit(0); to not display the output. This ends the user being able to input to loop.

I'm not sure how to fix my while loop. When I run the program it will only run once and then loop once. After that it will automatically loop without prompting and spits out the lines on one line like:

Would you like to calculate again (y/n):
Enter loan amount:

Code:

package whileloopyn;

import java.util.Scanner;

public class Whileloopyn {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    //Declare Variables
    double loanNum;
    double rateNum;
    double trateNum;
    double yearNum;
    double monthNum;
    double equNum;
    double outputNum;

    //Prompt for input and try catch for exception error (rate inputed as two)

    System.out.print("Enter loan amount: ");
    loanNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter rate: ");
    rateNum = Double.parseDouble(in.nextLine());

    System.out.print("Enter number years: ");
    yearNum = Double.parseDouble(in.nextLine());


    //Postive input data check (negative then ask again)
    if(loanNum <= 0 || rateNum <=0 || yearNum <=0)
    {
        System.out.println("You must enter positive numeric data! ");
        System.exit(0);
    }


    //Convert years to months 
    monthNum = yearNum * 12;

    //Convert rate to percent and monthly
    trateNum= (rateNum / 100) /12; 

    //Complete numerator 
    equNum = Math.pow((1+ trateNum), monthNum );

    //Plug equNum into formula
    outputNum = trateNum * loanNum * (equNum / (equNum - 1));

    //output answer
    System.out.printf("The monthly payment is: $ %.2f%n", outputNum);

    //prompt user y n to run program again
    System.out.println("Would you like to calculate again (y/n): ");
    String loop = in.nextLine();

    //while loop with (input

    boolean isContinuing = true;

    while (isContinuing) {
        while(loop.equals("y")){
        System.out.print("Enter loan amount: ");
        loanNum = in.nextDouble();

        System.out.print("Enter rate: ");
        rateNum = in.nextDouble();

        System.out.print("Enter number years: ");
        yearNum = in.nextDouble();

        //Postive input data check (negative then loop again)
        if(loanNum <= 0 || rateNum <=0 || yearNum <=0)
        {
           System.out.println("You must enter positive numeric data! ");
           System.exit(0);
        }


        //Convert years to months 
        monthNum = yearNum * 12;

        //Convert rate to percent and monthly
        trateNum= (rateNum / 100) /12; 

        //Complete numerator 
        equNum = Math.pow((1+ trateNum), monthNum );

        //Plug equNum into formula
        outputNum = trateNum * loanNum * (equNum / (equNum - 1));

        //output answer
        System.out.printf("The monthly payment is: $ %.2f%n", outputNum);
        //ask user if you want to use program again
        System.out.printf("Would you like to calculate again (y/n): ");

        if(loop.equals("n"))
        {
            System.exit(0);
        }
    }
    }

}

}

It was suggested that (input.equals(String)) is how I should compare the strings for the while loop and use in.nextLine();.

Martin Evans
  • 37,882
  • 15
  • 62
  • 83
  • you actually need to get the input if you ask the user and not just straight go into the if condition. Also you need to catch the carriage return after using any of the `nextFoo`(excluding `nextLine`) methods by calling `nextLine`. For reference of this check [this](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – SomeJavaGuy Oct 20 '15 at 05:55

1 Answers1

1

If user presses Y you have to continue that loop :

        System.out.printf("Would you like to calculate again (y/n): ");
        Scanner s = new Scanner(System.in);
        if(s.nextLine().equalsIgnoreCase("y")) {
            continue;
        } else {
            break;
        }

And Instead of while(loop.equals("y")){ you should write while(true) { because you have already checked that inside that loop

Rehman
  • 3,465
  • 3
  • 17
  • 31