Results 1 to 5 of 5
  1. #1
    kamikaze is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default 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!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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 --");
    }

  4. #4
    kamikaze is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    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 02:52 AM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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

  1. Replies: 45
    Last Post: 05-25-2010, 09:32 PM
  2. locale and decimalFormat confusion
    By bz3x in forum New To Java
    Replies: 2
    Last Post: 05-23-2010, 01:22 AM
  3. Replies: 2
    Last Post: 12-08-2009, 09:33 PM
  4. Simple program involving military time
    By busdude in forum New To Java
    Replies: 4
    Last Post: 10-08-2008, 07:03 PM
  5. DecimalFormat class
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 12-30-2007, 04:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •