my question is simple, how can i get my output to format two values with two decimal places. for whatever reason i cant output the "current balance" & "new balance" with two decimal places. separately they work fine but when together i get a conversion error. is this a rule i'm unaware of? i would like do do this with one action if possible. this is simply a cosmetic issue and all operations perform fine when i take out the formatting.
thanks,
public static void main(String[] args)
{
double min;
int accNum;
char accType;
double bal;
double newBal;
//user inputs
System.out.println("Please enter your account number: ");
accNum = console.nextInt();
System.out.println();
System.out.println("Enter the account type: s(savings) or c(checking)");
accType = console.next().charAt(0);
System.out.println();
//savings v checking
switch (accType)
{
case 's':
case 'S':
System.out.println("Enter the current balance: ");
bal = console.nextDouble();
System.out.println("Enter the minimum balance: ");
min = console.nextDouble();
System.out.println();
if (bal < min)
{
newBal = bal - S_MIN_FEE;
System.out.println("Insufficient Funds (-$10.00)");
}
else
newBal = bal + (bal * S_INT);
System.out.printf("Account #: " + accNum + "\n"
+ "Account Type: Savings" + "\n" + "Current Balance: "
+ "$%.2f%n", bal + "New Balance: $%.2f", newBal);
case 'c':
case 'C':
System.out.println("Enter the current balance: ");
bal = console.nextDouble();
System.out.println("Enter the minimum balance: ");
min = console.nextDouble();
System.out.println();
if (bal < min)
{
newBal = bal - C_MIN_FEE;
System.out.println("Insufficent Funds (-$25.00)");
}
else if (bal < C_BAL_MAX && bal >= min)
newBal = bal + (bal * C_INT);
else
newBal = bal + (bal * C_INT_MAX);
System.out.printf("Account #: " + accNum + "\n"
+ "Account Type: Checking" + "\n" + "Current Balance: "
+ "$%.2f%n", bal + "New Balance: $%.2f%n", newBal);
That is because you put the format first, and only first.
System.out.printf("%.2f %.2f%n", bal, newBal);
This is the same problem/mistake as posted an hour ago. java.util.IllegalFormatConversionException: f != java.lang.String Error
Related
hello I wrote a program to convert one temperature from one to another using switch.
the default does not fuction:
When i enter a valid option it works perfectly fine, but when i choose invalid choice it goes the default outputting the lines: You enter invalid choice, please choose again and stop workings without allowing me to choose another choice.
earlier when I tested it, the default allowed me to choose another option.
import java.util.Scanner;
public class Tempature {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double fahrenheit,celcius,kelvin;
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin");
String word = scan.nextLine();
switch(word){
case "f": System.out.println("Enter Fahrenheit temperature: ");
fahrenheit=scan.nextDouble();
celcius = (fahrenheit-32) * 5/9;
kelvin = (fahrenheit + 459.67) * 5/9;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
case "c": System.out.println("Enter the Celcius temperature: ");
celcius=scan.nextDouble();
fahrenheit = (celcius*9)/5 + 32;
kelvin = celcius + 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
case "k": System.out.println("Enter the Kelvin temperature: ");
kelvin=scan.nextDouble();
fahrenheit = 1.8*(kelvin - 273.15) + 32;
celcius = kelvin - 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
default: System.out.println("You enter invalid choice, please choose again");
}
scan.close();
}
}
This could help you.
add a while loop and read the input of user in default and it will work.
When user puts q he/she will exit the program.
class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double fahrenheit,celcius,kelvin;
// changed
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit");
String word = scan.next();
//added
while(!word.equals("q")) {
switch(word){
case "f": {
System.out.println("Enter Fahrenheit temperature: ");
fahrenheit=scan.nextDouble();
celcius = (fahrenheit-32) * 5/9;
kelvin = (fahrenheit + 459.67) * 5/9;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
case "c":{
System.out.println("Enter the Celcius temperature: ");
celcius=scan.nextDouble();
fahrenheit = (celcius*9)/5 + 32;
kelvin = celcius + 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
case "k": {
System.out.println("Enter the Kelvin temperature: ");
kelvin=scan.nextDouble();
fahrenheit = 1.8*(kelvin - 273.15) + 32;
celcius = kelvin - 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
default: {
System.out.println("You enter invalid choice, please choose again");
// added
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq.Quit");
word = scan.next();
break;
}
}// end of swtich
// checking for exit
if(!word.equals("q")) {
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit");
word = scan.next();
}
}// end of while
System.out.println("Exited");
}
}
Output:
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
f
Enter Fahrenheit temperature:
45
7.222222222222222 C
45.0 F
280.3722222222222 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
c
Enter the Celcius temperature:
45
45.0 C
113.0 F
318.15 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
k
Enter the Kelvin temperature:
45
-228.14999999999998 C
-378.66999999999996 F
45.0 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
cdvd
You enter invalid choice, please choose again
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q.Quit
q
Exited
I don't know why my code is not calculating the average. It is compiling and running, but gives 0% as average. Here are the instructions for the assignment
Average and Grade (Methods Program):
Write a program that asks the user to enter 5 test scores. Your program should then display a letter
grade for each test score, the average test score and the overall letter grade
for the average score. You can NOT use a for loop or an array (we haven't even covered that) in this
program - the point is to learn how to define methods and call them several
times if necessary.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Homework8{
static double average;
static char grade;
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
char letter1;
char letter2;
char letter3;
char letter4;
char letter5;
double score1;
double score2;
double score3;
double score4;
double score5;
double score;
System.out.println("Please enter the first score between 0 and 100: ");
score1 = keyboard.nextDouble();
System.out.println("Please enter the second score between 0 and 100: ");
score2 = keyboard.nextDouble();
System.out.println("Please enter the third score between 0 and 100: ");
score3 = keyboard.nextDouble();
System.out.println("Please enter the fourth score between 0 and 100: ");
score4 = keyboard.nextDouble();
System.out.println("Please enter the fifth score between 0 and 100: ");
score5 = keyboard.nextDouble();
//System.out.println("The average of your five tests was: " +
System.out.println(
"For TestA you scored: " + score1 + " giving you Grade: "
+ determineGrade(score1)
+ "\nFor TestB you scored: " + score2 + " giving you Grade: "
+ determineGrade(score2)
+ "\nFor TestC you scored: " + score3 + " giving you Grade: "
+ determineGrade(score3)
+ "\nFor TestD you scored: " + score4 + " giving you Grade: "
+ determineGrade(score4)
+ "\nFor TestF you scored: " + score5 + " giving you Grade: "
+ determineGrade(score5)
+ "\nBased on your average: " + average + " Your final Grade is: "
+ determineGrade((double)average));
}
public static double calcAverage(double score1, double score2, double score3, double score4, double score5) {
double average = (score1+score2+score3+score4+score5) / 5;
return average;
}
public static char determineGrade(double score)
{
if(score<=100 && score>=90)
{
return 'A';
}
else if(score>=80)
{
return 'B';
}
else if(score>=70)
{
return 'C';
}
else if(score>=60)
{
return 'D';
}
else
{
return 'F';
}
}
}
you define the method calcAverage but you didn't call it.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Homework8{
static double average;
static char grade;
public static void main (String[] args) {
Scanner keyboard = new Scanner(System.in);
char letter1;
char letter2;
char letter3;
char letter4;
char letter5;
double score1;
double score2;
double score3;
double score4;
double score5;
double score;
System.out.println("Please enter the first score between 0 and 100: ");
score1 = keyboard.nextDouble();
System.out.println("Please enter the second score between 0 and 100: ");
score2 = keyboard.nextDouble();
System.out.println("Please enter the third score between 0 and 100: ");
score3 = keyboard.nextDouble();
System.out.println("Please enter the fourth score between 0 and 100: ");
score4 = keyboard.nextDouble();
System.out.println("Please enter the fifth score between 0 and 100: ");
score5 = keyboard.nextDouble();
double average = calcAverage(score1,score2,score3,score4,score5);
//System.out.println("The average of your five tests was: " +
System.out.println(
"For TestA you scored: " + score1 + " giving you Grade: "
+ determineGrade(score1)
+ "\nFor TestB you scored: " + score2 + " giving you Grade: "
+ determineGrade(score2)
+ "\nFor TestC you scored: " + score3 + " giving you Grade: "
+ determineGrade(score3)
+ "\nFor TestD you scored: " + score4 + " giving you Grade: "
+ determineGrade(score4)
+ "\nFor TestF you scored: " + score5 + " giving you Grade: "
+ determineGrade(score5)
+ "\nBased on your average: " + average + " Your final Grade is: "
+ determineGrade((double)average));
}
public static double calcAverage(double score1, double score2, double score3, double score4, double score5) {
double average = (score1+score2+score3+score4+score5) / 5.0;
return average;
}
public static char determineGrade(double score)
{
if(score<=100 && score>=90)
{
return 'A';
}
else if(score>=80)
{
return 'B';
}
else if(score>=70)
{
return 'C';
}
else if(score>=60)
{
return 'D';
}
else
{
return 'F';
}
}
}
I cannot get the variable adjustedCharge to print out (the text is printed but not the value) I have tried to trace but still cannot get it to print properly. No errors are present either.
import java.util.Scanner;
public class Assignment {
public static void main(String[] args) {
//Declare scanner
Scanner input = new Scanner(System.in);
//Prompt for information
System.out.print("Customer's name:");
String name = input.nextLine();
System.out.print("Customer's address:");
String address = input.nextLine();
System.out.print("Customer's phone number:");
String phone = input.nextLine();
System.out.print("Customer's licence number:");
String licence = input.nextLine();
System.out.print("Customer's credit card number:");
String ccard = input.nextLine();
System.out.print("Credit card expiry date (MM/YYYY):");
String expiry = input.nextLine();
System.out.print("Hire length (in days):");
int hire = Integer.parseInt(input.nextLine());
System.out.print("Make/Model:");
String model = input.nextLine();
System.out.print("Registration of car:");
String rego = input.nextLine();
System.out.print("Hire rate (per day) Either S, M, L:");
char inputRate = input.nextLine().charAt(0);
System.out.print("Total days hired out:");
int totalDays = Integer.parseInt(input.nextLine());
//
double dtotalDays = totalDays;
double surcharge = dtotalDays - hire;
//Daily hire rate / Stage 2
double rate = 0;
if (inputRate == 'S' || inputRate == 's'){
rate = (char) 80.0;
}
if (inputRate == 'M' || inputRate == 'm'){
rate= 110.0;
}
if (inputRate == 'L' || inputRate == 'l'){
rate= 140.0;
}
//Calculate late fees
double penalty = rate * (surcharge * 2);
//Start Stage 3
double dCost=0;
double tCost=0;
StringBuilder dDetail = new StringBuilder("The damage Details are:" + "\n");
StringBuilder tDetail = new StringBuilder("The traffic fines are:" + "\n");
//Setup an exit statement
boolean quit = false;
while (!quit){
System.out.println("<<<Enter one of the following commands:>>>");
System.out.println("A - Damage Repair");
System.out.println("B - Traffic Infringement");
System.out.println("X - Exit Menu");
String schoiceEntry = input.next();
char choiceEntry = schoiceEntry.charAt(0);
//Integer.parseInt(input.nextLine());
double damageCost = 0;
switch (choiceEntry){
case 'A':
case 'a':
input.nextLine();
System.out.println("Enter a description of the damage:");
String damageDetail = input.nextLine();
System.out.println("Enter the damage cost:");
damageCost = input.nextInt();
System.out.print("The damage is: " + damageDetail + "\n");
System.out.print("The damage cost is: " + "$" + damageCost + "\n");
dDetail.append("-" + damageDetail + "\n");
dCost = dCost + damageCost;
System.out.println("----------------------------------");
break;
case 'B':
case 'b':
input.nextLine();
System.out.print("Enter a description of the traffic infringement:");
String trafficDetail = input.nextLine();
System.out.println("Enter the traffic infringement cost:");
double trafficCost = Integer.parseInt(input.nextLine());
tDetail.append("-" + trafficDetail + "\n");
tCost = tCost + trafficCost;
System.out.println("----------------------------------");
break;
case 'X':
case 'x':
quit = true;
System.out.print("***Menu entry has been terminated***" + "\n");
//System.out.printf("Total traffic cost is: %75s\n", "$" + tCost + "\n");
//System.out.printf("Total Damage cost is: %77s\n", "$" + dCost + "\n");
//System.out.printf(tDetail + "\n");
//System.out.print(dDetail + "\n");
break;
default:
System.out.print("Please enter either a valid menu selection (A, B, or X):" + "\n");
break;
}
}
double dhire = hire;
double charge = dhire*rate;
double adjustedCharge = charge + penalty;
//Print summary
System.out.println("---------------------------------------------"+
"------------------------------------------------------\n" +
"***CUSTOMER DETAILS:***\n");
System.out.printf("Name: %93s\n", name);
System.out.printf("Address: %90s\n", address);
System.out.printf("Phone Number: %85s\n", phone);
System.out.printf("Licence Number: %83s\n", licence);
System.out.printf("Credit Card Number: %79s\n", ccard);
System.out.printf("Credit Card Expiry: %79s\n", expiry);
System.out.println( "\n***CAR HIRE DETAILS:***\n");
System.out.printf("Make/Model: %87s\n", model);
System.out.printf("Registration Number: %78s\n", rego);
System.out.printf("Hire Length (days): %79s\n", model);
System.out.printf("Daily Hire Rate: %82s\n", rate);
System.out.printf("Basic Hire Charge: %80s\n\n", charge);
System.out.printf("Days hired: %87s\n", totalDays);
if (totalDays == hire){
System.out.printf("Late Return Surcharge: %76s\n", "$0.00");
}
if (totalDays > hire){
System.out.printf("Late Return Surcharge: %76s\n", + penalty);
}
System.out.printf("Adjusted Hire Charge: %77s\n", "\n", "$" + adjustedCharge + "\n");
System.out.print(dDetail + "\n");
System.out.printf("Total damage cost is: %78" + "s\n", "$" + dCost + "\n");
System.out.printf(tDetail + "\n");
System.out.printf("Total traffic fines incurred: %70s\n", "$" + tCost + "\n");
System.out.printf("Final hire charge: %79s\n", "$" + (adjustedCharge + dCost + tCost));
}
}
I just changed the way you printed out the adjustedCharge so you can still use printf
System.out.printf("Adjusted Hire Charge: %77s","$");
System.out.printf("%.1f",adjustedCharge);
System.out.printf("\n");
With printf you need to format the value you're printing and in this case, because you're trying to print a double, you'll need to format it with %f
I also noticed that my previous answer messed up the spacing you had for the rest of your output. So here's the fixed solution! Just change up the spacing to however you'd like
Or you can
use
Pass two arguments to printf
System.out.printf("Adjusted Hire Charge: %77s\n", "$" + adjustedCharge + "\n")
instead of
System.out.printf("Adjusted Hire Charge: %77s\n", "\n", "$" + adjustedCharge + "\n");
My program is supposed to show the calculation of the worker's total salary but somehow after all the input in include the program is end without showing any result.
It also to check if the total hours enter is over 40 and if does, it will calculate the overtime salary.
public class GajiTest {
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
class Salary {
public double CalculateGaji(int hours,int rate){
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
}}}
I suppose the problem add two lines try this :
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
Salary sal=new Salary();
sal.CalculateGaji(hours,rate,empName);
}
}
}
class Salary {
public void CalculateGaji(int hours,int rate,String empName){
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
Why it is not printing anything is because you are not calling your method CalculateGaji() of class Salary.
You need to call it by creating the object of the class Salary. (After the local class in the main method.)
Salary salary = new Salary();
salary.CalculateGaji(hours,rate);
Apart from it, there are a few things I would like to say,
You are creating an array of integers int[] arrayList= new int[numberOfEmp]; ArrayList is something else. What is an ArrayList? There should be no need to write the name of the variable to be arrayList. It might confuse you at some stage.
As per your code, you have created the class class Salary in your main method. This way it becomes the [Local Class][2]. If it is what you want then, it is find. Otherwise you could have written your class Salary out side main method and outside class class GajiTest.
But I don't see any need of creating any local class to achieve it. You can directly do it the following way,
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
int[] arrayList= new int[numberOfEmp];
for (int i = 0; i < arrayList.length; i++){
System.out.print("Enter Employee Name: ");
String empName= input.next();
System.out.print("Enter hourly rate: ");
int rate= input.nextInt();
System.out.print("Enter hours worked: ");
int hours=input.nextInt();
if (hours >=40)
{
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else
{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName+ "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
From your code, I think what you are trying to do is create an array of employees and print out their names and salaries. There are multiple errors in your code that will not allow it to function if that is your intention.
Here is the code to create an array of employees and print out their names and salaries based on your CalculateGaji function:
public class calculateSalary {
static class employee{
private double rate;
private double hours;
private String empName;
public employee(){}
public void setRate(double value){
rate = value;
}
public void setHours(double value){
hours = value;
}
public void setName(String value){
empName = value;
}
public void CalculateGaji(){
if (hours >=40){
double regPay= hours * rate;
double otPay = (hours-40) *(rate*1.5);
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName +"\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
else{
double regPay= hours * rate;
double otPay =0;
double totalPay= regPay + otPay;
System.out.print("\nEmployee name: " + empName + "\n Regular pay: " + regPay +"\n Overtime pay: " + otPay+ "\n Total pay: " + totalPay+ "\n"+ "\n");
}
}
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter number of Employees: ");
int numberOfEmp= input.nextInt();
employee[] employeeArray= new employee[numberOfEmp];
for (int i = 0; i < employeeArray.length; i++){
employeeArray[i] = new employee();
System.out.println("Enter Employee Name: ");
employeeArray[i].setName(input.next());
System.out.println("Enter hourly rate: ");
employeeArray[i].setRate(input.nextDouble());
System.out.println("Enter hours worked: ");
employeeArray[i].setHours(input.nextDouble());
}
for(int i = 0; i < employeeArray.length; i++){
employeeArray[i].CalculateGaji();
}
}
}
Hope this helps.
I am writing a simple loan calculator for a class. I am attempting to have the amounts return in dollar form. I have found system.out.printf(); but cannot seem to make it work properly.
Here is the current code: (I am also aware it doesn't work correctly, will fix later)
public static void calculate() {
annualInterest = (annualInterest * 0.01) / 12; //percentage to decimal then to month interest %
term = term * 12;
double payment = amtBorrowed * annualInterest;
double temp = 1/(1 + annualInterest);
temp = Math.pow(temp,term);
temp = 1 - temp;
payment = payment / temp;
double Interest = amtBorrowed * annualInterest;
double principal = payment - Interest;
System.out.println("Your monthly payment is " + payment);
System.out.println(" | Interest | principal");
System.out.println("Month 1 :" +" "+ Interest + " " + principal);
for (int i = 2; i < term + 1; i ++){
System.out.print("Month "+ i + " :");
amtBorrowed = amtBorrowed - (Interest + principal);
payment = amtBorrowed * annualInterest;
temp = 1/(1 + annualInterest);
temp = Math.pow(temp,term);
temp = 1 - temp;
payment = payment / temp;
Interest = amtBorrowed * annualInterest;
principal = payment - Interest;
System.out.println(" " + Interest + " " + principal);
}
}
Output:
Your monthly payment is 4432.061025275802
| Interest | principal
Month 1 : 500.0 3932.0610252758024
Month 2 : 477.839694873621 3757.789681084494
Month 3 : 456.6615479938304 3591.242149217312
Month 4 : 436.4220295077747 3432.0761055985745
Month 5 : 417.079538832243 3279.9643981645363
Month 6 : 398.5943191472591 3134.594374430564
Using printf instead
System.out.printf("Your monthly payment is $%.2f%n", payment);
This should print
Your monthly payment is $4432.06
Also
System.out.printf(" %8.2f %8.2f%n", Interest, principal);
check if this helps
DecimalFormat.getCurrencyInstance(Locale.US).format(doubleValue)