I have to make a calculator that will
Run program with a positive number input, display output and ask if user wants to loop.
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.
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();
.
nextFoo
(excludingnextLine
) methods by callingnextLine
. For reference of this check this – SomeJavaGuy