using the NumberFormat import in System.out.format - java

I'm doing a program on compound interest for a school assignment. I tried using System.out.format(); and used money.format to format the variables investment, interest, and investTotal. I don't know why but it keeps on throwing me an error for
"Invalid value type 'String' for format specifier '%.2f', parameter 2, 3, and 4" I've been trying to figure this out for quite a while now and I still can't seem to find why it is.
-- A
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// SPLASH
// CONSTANT
// OBJECT
Scanner input = new Scanner(System.in);
NumberFormat money = NumberFormat.getCurrencyInstance();
// VARIABLES
double investment;
double investTotal;
double rate;
double intrest;
int year;
// INPUT
do
{
System.out.print("Enter yearly investment (min $100.00 deposit): ");
investment = input.nextDouble();
}
while (investment < 100);
do
{
System.out.print("Enter intrest rate (%): ");
rate = input.nextDouble()/100;
}
while (rate <= 0);
do
{
System.out.print("Enter number of years: ");
year = input.nextInt();
}
while (year <= 0 || year > 15);
// PROCESSING
investTotal = investment;
for (int perYear = 1; perYear <= year; perYear++)
{
intrest = investTotal*rate;
investTotal = (investment+intrest);
System.out.format("%2s | %.2f | %.2f | %.2f\n", perYear, money.format(investment), money.format(intrest), money.format(investTotal));
investTotal = investTotal + investment;
}
// OUTPUT
}
}

getCurrencyInstance returns a String and therefor can't be formatted using %.2f.
You better look how NumberFormat works:
https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html
As you can see, the result of the formatting is a String, when you are using String.format with %.2f you should enter a number e.g:
System.out.format("%2s | %.2f\n", 1.001, 1.005);
I'm not sure what are you trying to get using the NumberFormat, if you classify I will be able to help you further with this question.

Related

Compounding interest calculator

My goal is to create a program that asks the user for an amount, asks for the interest rate per year, month or day, asks for how it will be compounded, then asks for the term in either months, days or years.
It ill then print the future value along with the total interest gained.
This is what I have so far and the numbers are incorrect.
if anyone could help revise this and make it work i would be very grateful.
import java.util.Scanner;
public class Compunding {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double compoundingTerms;
double period = 0;
System.out.println("Enter an amount of money: ");
double amount = sc.nextDouble();
System.out.println("Enter an rate of Interest: ");
double rate = sc.nextDouble();
System.out.println("Enter per years, months, or days: ");
String time = sc.next();
System.out.println("Enter how it will be componded monthly, semi-anually, quarterlly, anually: ");
String compoundRate = sc.next();
System.out.println("Enter the term amount: ");
double term = sc.nextDouble();
System.out.println("Enter the term type (Monthy,Yearly,Daily}: ");
String termType = sc.next();
if (time.equals("years")) {
period = 1;
}
if (time.equals("months")) {
period = 12;
}
if (time.equals("days")) {
period = 365;
}
if (compoundRate.equals("monthly")) {
rate = (rate / 100) / 12;
term = term * 12;
}
if (compoundRate.equals("semi-anually")) {
rate = (rate / 100) / 2;
term = term * 2;
}
if (compoundRate.equals("quarterlly")) {
rate = (rate / 100) / 4;
term = term * 4;
}
if (compoundRate.equals("anually")) {
rate = rate / 100;
term = term * 1;
}
double compoundPayment = 0;
for (int i = 1; i <= term; i++ ) {
if (i % period == 0 ) {
colInterest(amount, rate);
}
compoundPayment = amount * (1.0 + rate);
}
System.out.println("The Final payment will be: " + compoundPayment);
}
public static double colInterest(double valueAmount, double valueInterest) {
return valueAmount * valueInterest;
}
}
So there were a number of issues with the original calculation and what was posted. compoundPayment was set outside the for loop, and only once, so that compounding did not occur. Also, the term type was requested but not used, so every term was assumed to be years. I think it's also just hard to follow the logic of the for loop with the mod (I get it, that when we hit a day on which things are compounded, we give interest), but it's tricky to keep track of the various units (so I went for years, but one could make a case for days and a loop like yours). I did simplify and assume the rate given was annual, but you could make it daily and multiply by 365, or monthly and multiply by 12, or, just make sure your period and days have the same unit.
It's also the case that the choice of Double as opposed to BigDecimal to represent the money is one where I followed you lead and am answering the question asked. I'm not arguing what I'm answering here is the best possible approach (and one could enhance by using Currency as opposed to assuming it's in dollars).
One different approach would be to use exponents to work with repeated multiplications, or, even if not, to simplify the for loop (which allows you to do things like print statements along the way and allow for rounding of currency).
I am not fixing potential enhancements like that there aren't always 365 days in a year or formatting the decimals nicely or checking input more vigorously. I am trying to give a sense of a possible way to go.
One subtlety is the cast to (int) for numPeriods, which will, assuming the other parts worked (and I tested that 364 days compounded annually gave no interest, but 365 did), make sure not to give partial interest for periods not completed.
I hope that helps.
import java.util.Scanner;
public class Compounding {
private Scanner sc;
Compounding() {
sc = new Scanner(System.in);
}
public double getAmount() {
//enhancement: catch number format exceptions, negative numbers, etcetera, and presumbaly use a loop to retry
System.out.println("Enter an amount of money: ");
return sc.nextDouble();
}
//return interest as a rate
public double getInterestRate() {
//enhancement, validate input, catch errors
System.out.println("Enter an annual percent rate of interest: ");
double rate = sc.nextDouble();
return rate / 100;
}
public int getTimesCompoundedPerYear() {
System.out.println("Enter how it will be componded monthly, semi-anually, quarterly, anually: ");
String compoundRate = sc.next();
if (compoundRate.equals("monthly")) {
return 12;
} else if (compoundRate.equals("semi-anually")) {
return 2;
} else if (compoundRate.equals("quarterly")) {
return 4;
} else if (compoundRate.equals("annually")) {
return 1;
} else {
System.out.println("Unrecognized compounding, defaulting to monthly");
return 12;
}
}
//return term amount, units still tbd
//allowing for decimals in case someone says 6.5 years for dsomey=thing compounded more than once a year
public double getTermAmount() {
//enhancement, validate input, catch errors
System.out.println("Enter term amount: ");
return sc.nextDouble();
}
public String getTermUnits() {
System.out.println("Enter the term type (years, months, days): ");
String termType = sc.next();
if (termType.equals("years") || termType.equals("months") || termType.equals("days")) {
return termType;
} else {
System.out.println("Unrecognized time period, defaulting to years.");
return "years";
}
}
public static void main(String[] args) {
Compounding compounding = new Compounding();
double period = 12;
double amount = compounding.getAmount();
double annualRate = compounding.getInterestRate(); //interest rates are always quoted as annual, no need to vary that
int timesCompoundedPerYear = compounding.getTimesCompoundedPerYear();
double term = compounding.getTermAmount();
String termUnits = compounding.getTermUnits();
double ratePerPeriod = annualRate / timesCompoundedPerYear;
double timeInYears = term;
if (termUnits.equals("months")) {
timeInYears /= 12;
} else if (termUnits.equals("days")) {
timeInYears /= 365;
}
int numPeriods = (int) timeInYears * timesCompoundedPerYear;
double compoundPayment = amount * Math.pow(1 + ratePerPeriod, numPeriods);
System.out.println("The Final payment will be: " + compoundPayment);
}
}

Proper Formatting

I have the program working I just need help cutting off the extra numbers, Im not very skilled at using the printf statements when printing in Java. When I run it I get output like 1225.043 Here is what I have:
import java.util.Scanner;
public class Comparison {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
float amount;
double principal = 1000.00;
double rate;
System.out.println("Enter interest rate");
rate = keyboard.nextDouble();
System.out.println("Year" +" "+ "Amount on deposit");
for(int year = 1; year <= 10; ++year)
{
amount = (float) (principal * Math.pow(1.0 + rate, year));
System.out.println(year+ " "+ amount);
System.out.println();
}
}
}
Try
System.out.printf("%2d %.2f%n", year, amount);
Output:
Enter interest rate
0.1
Year Amount on deposit
1 1100.00
2 1210.00
3 1331.00
4 1464.10
5 1610.51
6 1771.56
7 1948.72
8 2143.59
9 2357.95
10 2593.74

monthly payment calculator

I have some code which I find to keep giving me a dividing by 0 error.
It is suppose to calculate the monthly payment amount!
import java.io.*;
public class Bert
{
public static void main(String[] args)throws IOException
{
//Declaring Variables
int price, downpayment, tradeIn, months,loanAmt, interest;
double annualInterest, payment;
String custName, inputPrice,inputDownPayment,inputTradeIn,inputMonths, inputAnnualInterest;
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
//Get Input from User
System.out.println("What is your name? ");
custName = dataIn.readLine();
System.out.print("What is the price of the car? ");
inputPrice = dataIn.readLine();
System.out.print("What is the downpayment? ");
inputDownPayment = dataIn.readLine();
System.out.print("What is the trade-in value? ");
inputTradeIn = dataIn.readLine();
System.out.print("For how many months is the loan? ");
inputMonths = dataIn.readLine();
System.out.print("What is the decimal interest rate? ");
inputAnnualInterest = dataIn.readLine();
//Conversions
price = Integer.parseInt(inputPrice);
downpayment = Integer.parseInt(inputDownPayment);
tradeIn = Integer.parseInt(inputTradeIn);
months = Integer.parseInt(inputMonths);
annualInterest = Double.parseDouble(inputAnnualInterest);
interest =(int)annualInterest/12;
loanAmt = price-downpayment-tradeIn;
//payment = loanAmt*interest/a-(1+interest)
payment=(loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,-months)))));
//Output
System.out.print("The monthly payment for " + custName + " is $");
System.out.println(payment);
// figures out monthly payment amount!!!
}
}
the problem occurs when attempting to set the payment variable.
i don't understand why it keeps coming up with dividing by 0 error.
You have declared your variables as Int so 1/interest and 1/(interest*Math.pow(1+interest,-months)) will return 0. Change the type of your variables to float or double.
One suggestion to you, is that you should learn to "backwards slice" your code.
This means that when you see that you're getting a DivideByZeroException you should look at your code, and say, "why could this happen?"
In your case, let's look at this:
payment=(loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,-months)))));
So, now, Math.pow will never return anything zero (as it's a power), so it must be the case that interestis zero. Let's find out why:
interest =(int)annualInterest/12;
So now, integer division in Java truncates. This means that if you have .5 it will be cut off, and turned into zero. (Similarly, 1.3 will be truncated to 0).
So now:
annualInterest = Double.parseDouble(inputAnnualInterest);
This implies that you are passing in something that gets parsed to a value that is less than 12. If it were greater than 12 then you would get something else.
However, you might just be passing in an invalid string, for example, passing in "hello2.0" won't work!
This will be rounding always to 0. So it is trowing exception.
(1/interest)-(1/(interest*Math.pow(1+interest,-months)))));
Use float type instead of int. Learn how they works.
package computeloan;
import java.util.Scanner;
public class ComputeLoan {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(" Enter Yearly Interest Rate : ");
double annualIntersetRate = input.nextDouble();
double monthlyIntersetRate = annualIntersetRate / 1200;
System.out.print(" Enter Number of years : ");
int numberOfYears = input.nextInt();
// Enter loan amount
System.out.print(" Enter Loan Amount : ");
double loanAmount = input.nextDouble();
double monthlyPayment = loanAmount * monthlyIntersetRate /(1-1/Math.pow(1+monthlyIntersetRate,numberOfYears*12 ));
double totalPayment = monthlyPayment * numberOfYears * 12;
//Calculate monthlyPaymeent and totalPayment
System.out.println(" The Monthly Payment Is : " +(int)(monthlyPayment*100) /100.0);
System.out.println(" The Total Payment Is : " +(int)(totalPayment*100) /100.0 );
}
}

Java loan calculator

I'm trying to code a loan calculator. I seem to be having issues. I am trying to get an input from the user and validate the input. I know I am doing it wrong the problem is I'm scratching my head wondering how to do it right.
I get a red line on the d = getDouble(sc, prompt); and the i = getInt(sc, prompt); which I understand I don't have that coded correctly. I'm just unsure how to go about fixing it.
I also have to validate the continue statement which I wasn't to sure the best way to go about that and finally the instructor expects the code to be 80 lines or less which I am right about 80 lines. I guess I'm looking for a better way to do this but being new I'm scratching my head and I'm hoping someone can lend a hand.
As always I really appreciate the help.
import java.util.Scanner;
import java.text.NumberFormat;
public class LoanCalculator
{
public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max)
{
double d = 0.0;
boolean isValid = false;
while(isValid == false);
{
d = getDouble(sc, prompt);
if (d <= min)
{
System.out.println("Error! Number must be greater tha 0.0");
}
else if (d >= max)
{
System.out.println("Error number must be less than 1000000.0");
}
else
isValid = true;
}
return d;
}
public static int getIntWithinRange(Scanner sc, String prompt, 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 more than 0");
else if (i >= max)
System.out.println("Error! Number must be less than 100");
else
isvalid = true;
}
}
public static void main(String[] args)
{
System.out.println("Welcome to the loan calculator");
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y"))
{
System.out.println("DATA ENTRY");
double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0.0, 1000000.0);
double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 20);
int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);
int months = years * 12;
double monthlyPayment = loanAmount * interestRate/
(1 - 1/Math.pow(1 + interestRate, months));
NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMaximumFractionDigits(3);
System.out.println("RESULST");
System.out.println("Loan Amount" + currency.format(loanAmount));
System.out.println("Yearly interest rate: " + percent.format(interestRate));
System.out.println("Number of years: " + years);
System.out.println("Monthly payment: " + currency.format(monthlyPayment));
System.out.println();
System.out.println("Continue? (y/n): ");
choice =sc.next();
System.out.println();
}
}
}
You haven't made the implementation of your getDouble(Scanner,String) and getInt(Scanner,String) that's why you're getting the red line.
since you already have a scanner, and prompt string change it to this
System.out.print(prompt);
d = sc.nextDouble();
and for the integer
System.out.print(prompt);
i = sc.nextInt();
I think getDouble and getInt are string functions so you would have to get a string first then call those methods. However, since you have a scanner, I assume you want to use that with the nextXXX methods:
Scanner sc = new Scanner (System.in);
double d = sc.nextDouble();
You can use this complete snippet for educational purposes:
import java.util.Scanner;
class Test {
public static void main (String args[]) {
Scanner sc = new Scanner (System.in);
System.out.print("Enter your double: ");
double d = sc.nextDouble();
System.out.print("Enter your integer: ");
int i = sc.nextInt();
System.out.println("You entered: " + d + " and " + i);
}
}
Transcript:
Enter your double: 3.14159
Enter your integer: 42
You entered: 3.14159 and 42
Basically, the process is:
Instantiate a scanner, using the standard input stream.
Use print for your prompts.
Use the scanner nextXXX methods for getting the input values.
A little more assistance here, based on your comments.
In your main function, you have:
double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ", 0.0, 1000000.0)
and that function has the prototype:
public static double getDoubleWithinRange(
Scanner sc, String prompt, double min, double max)
That means those variables in the prototype will be set to the values from the call. So, to prompt for the information, you could use something like (and this is to replace the d = getDouble(sc, prompt); line):
System.out.print(prompt);
double d = sc.nextDouble();
And there you have it, you've prompted the user and input the double from them. The first line prints out the prompt, the second uses the scanner to get the input from the user.
As an aside, your checks for the minimum and maximum are good but your error messages have hard-coded values of 0 and 100K. I would suggest that you use the parameters to tailor these messages, such as changing:
System.out.println("Error! Number must be greater tha 0.0");
into:
System.out.println("Error! Number must be greater than " + min);
That way, if min or max change in future , your users won't get confused :-)
I'll leave it up to you to do a similar thing for the integer input. It is your homework, after all :-)

Taking a users input in a library class and making sure it is positive

Here are my two java files that I am working with. The problem I am having is getting the getPosNum method (highlighted below) to take the number the user inputs and see if that number is positive rather than doing what is doing right now, which is:
jGRASP exec: java Prog12
OUTPUT: First Investment
INPUT: Please enter a positive number: -5000.00
INPUT: ERROR: -5000.0 is not positive; try again: -3000.00
Rather than getting it to say something like this:
jGRASP exec: java Prog12
OUTPUT: First Investment
INPUT: Enter the first principal amount: -5000.00
INPUT: ERROR: -5000.0 is not positive; try again: -3000.00
How can I fix this problem and make it read correctly? Am I making sense?
public class Finance
{
public static double getPosNum (String prompt)
{
double num;
num = Input.readDouble("Please enter a positive number: ");
while (num <= 0.0)
num = Input.readDouble("ERROR: " + num +
" is not positive; try again: ");
Output.showValue("You entered ", num);
return num;
} // method getPosNum
public static void outFinances (double prin, double rate, double years, double fv)
{
Output.showMessage("The original amount invested was $" + prin + ",\nand the annual interest rate was set at " + rate + "%.\nIt has been " + years + " years since the investment was made,\nand the future value of the investment after that many years is $" + fv + ".");
} // method outFinances
public static double futureValue (double prin, double rate, double years)
{
double FV, P, r, n;
P = prin;
r = rate;
n = years;
FV = P * Math.pow((1 + (r / 100)),(n));
return FV;
} // method outFinances
} // class Finance
// File: Prog12.java
// CS200 Lab 12 Main
// Author: Ryan Pech
// Created: February 19, 2011
public class Prog12
{
public static void main (String [] args)
{
double p, r, y, fv;
Output.showMessage("First Investment");
p = Finance.getPosNum("Enter the first principal amount: ");
r = Finance.getPosNum("Enter the first interest rate: ");
y = Finance.getPosNum("Enter the first number of years: ");
fv = Finance.futureValue(p, r, y);
Finance.outFinances(p, r, y, fv);
Output.showMessage("Second Investment");
p = Finance.getPosNum("Enter the second principal amount: ");
r = Finance.getPosNum("Enter the second interest rate: ");
y = Finance.getPosNum("Enter the second number of years: ");
fv = Finance.futureValue(p, r, y);
Finance.outFinances(p, r, y, fv);
} // method main
} // class Prog12
// File: Finance.java
// CS200 Lab 12
// Author: Ryan Pech
// Created: February 19, 2011
Your getPostNum() accepts a parameter called prompt, but you never use that variable in that method at all. Instead of hardcoding Please enter a positive number:, substitute that string with that prompt variable, like this:-
Change...
num = Input.readDouble("Please enter a positive number: ");
... to...
num = Input.readDouble(prompt);
It should be obvious that it's printing "Please enter a positive number: " because you explicitly told it to in the getPosNum() method.
Using
num = Input.readDouble(prompt)
will solve your problem. A better way to go about this is to restrict printing to your main() method and use getPosNum() for input only. This way you know explicitly what you are printing every time. Something like
System.out.println("Enter the first principle amount.");
num = Input.readDouble();
and then perform some negative number checks.

Categories

Resources