Question
Need some help or guidance to modify the getDouble() method in the console class so it can remove any $ or % signs from the
Need some help or guidance to modify the getDouble() method in the console class so it can remove any $ or % signs from the string that is put in by the user.
FutureValueApp Code:
import java.util.*;
import java.text.*;
public class FutureValueApp {
public static void main(String[] args) {
// display a welcome message
System.out.println("Welcome to the Future Value Calculator");
System.out.println();
// perform 1 or more calculations
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
// get the input from the user
System.out.println("DATA ENTRY");
double monthlyInvestment = Console.getDouble(
"Enter monthly investment: ", 0, 1000);
double interestRate = Console.getDouble(
"Enter yearly interest rate: ", 0, 30);
int years = Console.getInt(
"Enter number of years: ", 0, 100);
// calculate the future value
double monthlyInterestRate = interestRate/12/100;
int months = years * 12;
double futureValue = calculateFutureValue(
monthlyInvestment, monthlyInterestRate, months);
// get the currency and percent formatters
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(1);
// format the result as a single string
String results =
"Monthly investment:\t"
+ currency.format(monthlyInvestment) + "\n"
+ "Yearly interest rate:\t"
+ percent.format(interestRate/100) + "\n"
+ "Number of years:\t"
+ years + "\n"
+ "Future value:\t\t"
+ currency.format(futureValue) + "\n";
// print the results
System.out.println();
System.out.println("FORMATTED RESULTS");
System.out.println(results);
// see if the user wants to continue
choice = Console.getString("Continue? (y/n): ");
System.out.println();
}
}
public static double calculateFutureValue(double monthlyInvestment,
double monthlyInterestRate, int months) {
Console Code:
import java.util.Scanner;
public class Console {
private static Scanner sc = new Scanner(System.in);
public static String getString(String prompt) {
System.out.print(prompt);
String s = sc.next(); // read user entry
sc.nextLine(); // discard any other data entered on the line
return s;
}
public static int getInt(String prompt) {
int i = 0;
boolean isValid = false;
while (!isValid) {
System.out.print(prompt);
if (sc.hasNextInt()) {
i = sc.nextInt();
isValid = true;
} else {
System.out.println("Error! Invalid integer. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public static int getInt(String prompt, int min, int max) {
int i = 0;
boolean isValid = false;
while (!isValid) {
i = getInt(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 double getDouble(String prompt) {
double d = 0;
boolean isValid = false;
while (!isValid) {
System.out.print(prompt);
if (sc.hasNextDouble()) {
d = sc.nextDouble();
isValid = true;
} else {
System.out.println("Error! Invalid number. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
public static double getDouble(String prompt, double min, double max) {
double d = 0;
boolean isValid = false;
while (!isValid) {
d = getDouble(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;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
o modify the getDouble method in the Console class to remove any or signs from the input string you ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started