Results 1 to 5 of 5
Thread: Error involving DecimalFormat
- 10-18-2010, 12:07 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Error involving DecimalFormat
Hey guys, I'm a beginning java user and I'm working on a programming assignment that asks me to write a program that takes as input the marital status ("single" or "married", case insensitive) and the taxable income (double), and compute taxes.
My program was able to compile, but when I inputted a value for taxable income, I am receiving the following error message:
Enter your marital status (single or married):
single
Enter your taxable income:
10000
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number
at java.text.DecimalFormat.format(DecimalFormat.java: 487)
at java.text.Format.format(Format.java:140)
at Tax.main(Tax.java:73)
Here is my program:
// This program calculates the user's income tax
import java.util.Scanner;
import java.text.DecimalFormat;
public class Tax
{
public static void main(String [] args)
{
// Create a scanner object to read from the keyboard
Scanner keyboard = new Scanner(System.in);
// Create a DecimalFormat object
double tax;
DecimalFormat formatter = new DecimalFormat("0.00");
// Identifier declarations
double income;
String single, married;
// Marital Status: Single or Married
System.out.println("Enter your marital status (single or married): ");
String status = keyboard.nextLine();
if(status.equalsIgnoreCase("single"));
else if(status.equalsIgnoreCase("married"));
else
System.out.println("-- illegal marital status --");
// Taxable Income
System.out.println("Enter your taxable income: ");
income = keyboard.nextDouble();
// Marital Status: Single
if(status.equals("Single"));
else if(income > 0 && income <= 8000) {
tax = (income*.10);
} else if(income > 8000 && income <= 32000) {
tax =((income - 8000)*.15 + 800);
} else if(income > 32000) {
tax = ((income - 32000)*.25 + 4400);
} else {
System.out.println("-- illegal income --");
}
// Marital Status: Married
if(status.equals("Married"));
else if(income > 0 && income <= 16000) {
tax = (income*.10);
} else if(income > 16000 && income <= 64000) {
tax = ((income - 16000)*.15 + 1600);
} else if(income > 64000) {
tax = ((income - 64000)*.25 + 8800);
} else
System.out.println("-- illegal income --");
// Display Results
System.out.println("Your income tax is $" + formatter.format("tax"));
}
}
Any help or suggestions would be greatly appreciated!
- 10-18-2010, 12:26 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
So which is line 73?
I'm guessing it's
Java Code:System.out.println("Your income tax is $" + formatter.format("tax"));
The reason for the error is that format() expects a number (a numeric value) as an argument. It will format that numeric value: ie return a string representation. Now you are actually passing a three letter string as the argument. And format() has no way interpreting the three letters t-a-x as a number.
- 10-18-2010, 12:32 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Also could you tidy up the code?
Use the # button in the advanced reply pane so that your code appears within [CODE] and [/CODE] tags.
Also some of the lines look a bit odd (if blocks that do nothing):
Java Code:String status = keyboard.nextLine(); if(status.equalsIgnoreCase("single")); else if(status.equalsIgnoreCase("married")); else System.out.println("-- illegal marital status --");
is better written as
Java Code:String status = keyboard.nextLine(); if(!status.equalsIgnoreCase("single") && !status.equalsIgnoreCase("married")) { System.out.println("-- illegal marital status --"); }
or
Java Code:String status = keyboard.nextLine().toLowerCase(); if(!status.equals("single") && !status.equals("married")) { System.out.println("-- illegal marital status --"); }
- 10-18-2010, 01:45 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
Oh ok thanks! This is my first time using this forum so I was unfamiliar with the CODE tags. Anyways, I reformatted my program and was able to fix the issue as you mentioned earlier with line 73. I compiled and tested the program a few times, but some of the results did not come out correctly. For some reason, when I input "single", the answer is wrong, versus "married" where it is coming out correct. I checked the formulas, but they look fine.
Java Code:Enter your marital status (single or married): single Enter your taxable income: 10000 Your income tax is $1000.0 The real answer is $1100.00 Enter your marital status (single or married): married Enter your taxable income: 64001 Your income tax is $8800.25 // This program calculates the user's income tax import java.util.Scanner; import java.text.DecimalFormat; public class Tax { public static void main(String [] args) { // Create a scanner object to read from the keyboard Scanner keyboard = new Scanner(System.in); // Create a DecimalFormat object DecimalFormat formatter = new DecimalFormat("0.00"); // Identifier declarations double tax = 0; double income; String single, married; // Marital Status: Single or Married System.out.println("Enter your marital status (single or married): "); String status = keyboard.nextLine(); if(!status.equalsIgnoreCase("single") && !status.equalsIgnoreCase("married")) { System.out.println("-- illegal marital status --"); } // Taxable Income System.out.println("Enter your taxable income: "); income = keyboard.nextDouble(); // Marital Status: Single if(status.equals("Single")); else if(income > 0 && income <= 8000) { tax = (income*.10); } else if(income > 8000 && income <= 32000) { tax = (800 + (income - 8000)*.15); } else if(income > 32000) { tax = (4400 + (income - 32000)*.25); } else { System.out.println("-- illegal income --"); } // Marital Status: Married if(status.equals("Married")); else if(income > 0 && income <= 16000) { tax = (income*.10); } else if(income > 16000 && income <= 64000) { tax = (1600 + (income - 16000)*.15); } else if(income > 64000) { tax = (8800 + (income-64000)*.25); } else System.out.println("-- illegal income --"); // Display Results System.out.println("Your income tax is $" + tax); } }Last edited by kamikaze; 10-18-2010 at 01:52 AM.
- 10-18-2010, 02:04 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
You still have a couple of if blocks that appear o do nothing.
I suspect that when you write:
Java Code:// Marital Status: Single if(status.equals("Single")); // <--- ??? else if(income > 0 && income <= 8000) { tax = (income*.10); } else if(income > 8000 && income <= 32000) { tax = (800 + (income - 8000)*.15); } else if(income > 32000) { tax = (4400 + (income - 32000)*.25); } else { System.out.println("-- illegal income --"); } // Marital Status: Married if(status.equals("Married")); // <---- ??? else if(income > 0 && income <= 16000) { tax = (income*.10); } // etc etc etc
that you really mean something like
Java Code:// Marital Status: Single if(status.equals("Single")) { if(income > 0 && income <= 8000) { tax = (income*.10); } else if(income > 8000 && income <= 32000) { tax = (800 + (income - 8000)*.15); } else if(income > 32000) { tax = (4400 + (income - 32000)*.25); } else { System.out.println("-- illegal income --"); } } else if(status.equals("Married")) { // Marital Status: Married if(income > 0 && income <= 16000) { tax = (income*.10); } // etc etc etc
Similar Threads
-
Question involving Polymorphism, inherticance, casting...
By myst in forum New To JavaReplies: 45Last Post: 05-25-2010, 08:32 PM -
locale and decimalFormat confusion
By bz3x in forum New To JavaReplies: 2Last Post: 05-23-2010, 12:22 AM -
Dealing with exceptions in my simple GUI app involving a process
By fawkes711 in forum AWT / SwingReplies: 2Last Post: 12-08-2009, 08:33 PM -
Simple program involving military time
By busdude in forum New To JavaReplies: 4Last Post: 10-08-2008, 06:03 PM -
DecimalFormat class
By Java Tip in forum Java TipReplies: 1Last Post: 12-30-2007, 03:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks