Results 1 to 4 of 4
  1. #1
    roaan is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Lightbulb using numberformat

    Hi ,

    I am trying to use number format in java to achieve the same functionality of that of Integer.parseInt(String s)

    String s = "845";
    int x = Integer.parseInt(s);
    System.out.println(x);

    NumberFormat formatter = NumberFormat.getNumberInstance();
    Number n = formatter.parse(s); // I get an error here.

    What is the issue here. So how do we use numberformat to get the same function?

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Number parse(String source) - Parses text from the beginning of the given string to produce a number.
    try
    Long x = (Long) formatter.parse("123");

    EDIT:
    what kind of error? did you catch the exception?
    Last edited by angryboy; 07-01-2009 at 10:28 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    roaan is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Smile works now

    Hi,

    It works now as you pointed it out. I was not catching the ParseException as well.

    Thanks,
    Rohan.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. NumberFormat is abstract?
    By DrKush in forum New To Java
    Replies: 4
    Last Post: 03-27-2009, 08:54 AM
  2. Parsing a number using a NumberFormat
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-17-2008, 12:03 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
  •