Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    62
    Rep Power
    0

    Default Cannot format given Object as a Number

    As you can see on line 5 of the following code, I have indeed imported the NumberFormat class:

    Java Code:
    package toyz;
    
    import java.util.Scanner;//import Scanner class from java.util package
    import java.lang.Double;
    import java.text.NumberFormat;
    
    public class Toyz {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		Scanner sc = new Scanner(System.in);//create a scanner object named sc
    		
    		System.out.print("Enter product code: ");
    		String productCode = sc.nextLine();//returns the input as a string object.
    		
    		System.out.print("Enter product price: " );
    		String price = sc.nextLine();
    		
    		sc.close();
    		
    		//double convertedPrice = Double.parseDouble(price);
    		//System.out.print("The price for product " + productCode + " is $" + convertedPrice);
    		
    		NumberFormat currency = NumberFormat.getCurrencyInstance();
    		String priceFormatted = currency.format(price);
    		System.out.print("Again, the price for the product " + productCode + " is " + priceFormatted);
    
    	}
    
    }
    However, before I can finish testing the program, this error appears in the console:
    Java Code:
    Enter product code: 5580
    Enter product price: 234.567
    Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number
    	at java.text.DecimalFormat.format(Unknown Source)
    	at java.text.Format.format(Unknown Source)
    	at toyz.Toyz.main(Toyz.java:26)
    What am I missing?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Cannot format given Object as a Number

    The variable: price is a String not a number. Try converting the String to a numeric value.

    I don't know why the compiler doesn't give an error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,217
    Rep Power
    12

    Default Re: Cannot format given Object as a Number

    Hi Norm,

    This was interesting. I checked the source code for NumberFormat. It extends Format and uses the following directly.

    Java Code:
    public final String format (Object obj) {
            return format(obj, new StringBuffer(), new FieldPosition(0)).toString();
        }
    So the compiler isn't going to flag the String as a bad type.

    Imho and unless there is something subtle that I am missing, the NumberFormat class should have probably intercepted the call to a method like the following:

    Java Code:
    public <T extends Number> String MyFormat(T numb);
    I compile with Java 10 but I checked Java 8 and it still doesn't flag the error.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,959
    Rep Power
    31

    Default Re: Cannot format given Object as a Number

    Right. I forgot to look at the format method in the Format class.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    62
    Rep Power
    0

    Default Re: Cannot format given Object as a Number

    Quote Originally Posted by Norm View Post
    Right. I forgot to look at the format method in the Format class.
    It turns out the answer is a simple syntax one. What I needed was this:
    Java Code:
    System.out.print("Enter product price: " );
    		//String price = input.nextLine();
    		double price = input.nextDouble();
    It seems that the so-called getCurrencyInstance() is not capable of formatting strings, but doubles it can do.

Similar Threads

  1. Replies: 13
    Last Post: 04-26-2016, 04:12 PM
  2. Number Format Help
    By glockner in forum New To Java
    Replies: 11
    Last Post: 01-27-2015, 07:30 AM
  3. Number format Exception
    By know_how in forum Web Frameworks
    Replies: 2
    Last Post: 01-29-2013, 04:58 PM
  4. Number format Exception
    By aortell24 in forum New To Java
    Replies: 5
    Last Post: 07-16-2012, 05:22 AM
  5. Number format
    By asai in forum New To Java
    Replies: 12
    Last Post: 06-21-2012, 10:24 AM

Posting Permissions

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