Menu

Way Add Try Catch Code Import Javatextnumberformat Import Javautilscanner Public Class Fut Q43904105

Is there any way to add try andcatch into this code?

import java.text.NumberFormat;
import java.util.Scanner;

public class FutureValueApp {

public static void main(String[] args) {
System.out.println(“Welcome to the Future Value Calculatorn”);

Scanner sc = new Scanner(System.in);
String choice = “y”;
System.out.println(“DATA ENTRY”);
while (choice.equalsIgnoreCase(“y”)) {
/////get user input

double monthlyInvestment = getDoubleWithinRange(sc,
“Enter monthly investment: “, 0, 1000);
double interestRate = getDoubleWithinRange(sc,
“Enter yearly interest rate: “, 0, 30);
int years = getIntWithinRange(sc,
“Enter number of years: “, 0, 100);
System.out.println();

//////future value calculation
double monthlyInterestRate = interestRate / 12 / 100;
int months = years * 12;
double futureValue = calculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);

//////print results
System.out.println(“FORMATTED RESULTS”);
printFormattedResults(monthlyInvestment, interestRate,
years, futureValue);
  
////// see if user wants to continue
choice = askToContinue(sc);
}
}

public static double getDoubleWithinRange(Scanner sc, Stringprompt,
double min, double max) {
double d = 0;
boolean isValid = false;
while (isValid == false) {
d = getDouble(sc, prompt);
if (d <= min) {
System.out.println(
“Error! Number must be greater than ” + min + “.”);
} else if (d >= max) {
System.out.println(
“Error! Number must be less than ” + max + “.”);
} else {
isValid = true;
}
}
return d;
}

public static double getDouble(Scanner sc, String prompt){
double d = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNextDouble()) {
d = sc.nextDouble();
isValid = true;
} else {
System.out.println(“Error! Invalid number. Try again.”);
}
sc.nextLine(); //////discard other data entered on line
}
return d;
}

public static int getIntWithinRange(Scanner sc, Stringprompt,
int min, int max) {
int i = 0;
boolean isValid = false;
while (isValid == false) {
i = getInt(sc, prompt);
if (i <= min) {
System.out.println(
“Error! Number must be greater than ” + min + “.”);
} else if (i >= max) {
System.out.println(
“Error! Number must be less than ” + max + “.”);
} else {
isValid = true;
}
}
return i;
}

public static int getInt(Scanner sc, String prompt) {
int i = 0;
boolean isValid = false;
while (isValid == false) {
System.out.print(prompt);
if (sc.hasNextInt()) {
i = sc.nextInt();
isValid = true;
} else {
System.out.println(“Error! Invalid integer. Try again.”);
}
sc.nextLine(); //////discard other data entered on line
}
return i;
}

public static double calculateFutureValue(doublemonthlyInvestment,
double monthlyInterestRate, int months) {
double futureValue = 0;
for (int i = 1; i <= months; i++) {
futureValue = (futureValue + monthlyInvestment) *
(1 + monthlyInterestRate);
}
return futureValue;
}
  
public static String askToContinue(Scanner sc) {
System.out.print(“Continue? (y/n): “);
String choice = sc.next();
sc.nextLine(); ////// discard other data entered on line
System.out.println();
return choice;
}
  
private static void printFormattedResults(doublemonthlyInvestment,
double interestRate, int years, double futureValue) {
///// get percent and currency formatters
NumberFormat p = NumberFormat.getPercentInstance();
NumberFormat c = NumberFormat.getCurrencyInstance();
p.setMinimumFractionDigits(1);

///// format result as single string
String results
= “Monthly investment: ” + c.format(monthlyInvestment) + “n”
+ “Yearly interest rate: ” + p.format(interestRate / 100) +”n”
+ “Number of years: ” + years + “n”
+ “Future value: ” + c.format(futureValue) + “n”;
  
///// print results
System.out.println(results);
}
}

Expert Answer


Answer to Is there any way to add try and catch into this code? import java.text.NumberFormat; import java.util.Scanner; public cl…

OR