Results 1 to 11 of 11
Like Tree2Likes
  • 2 Post By JosAH

Thread: Parsing Integers

  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Parsing Integers

    Java Code:
    Integer.parseInt("4");
    This line throws exception, does it mean that only characters are parsed into Integers ? I think its a string and by parsing I can convert this string to integer value .

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,924
    Rep Power
    5

    Default Re: Parsing Integers

    I am not sure what you are asking. Your syntax is correct and that line should not throw an exception. Please include the relevant code.

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

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Parsing Integers

    ok wait ...

  4. #4
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Parsing Integers

    Java Code:
    Scanner input = new Scanner(System.in);
    		String string = input.next();
    		try {
    			Integer.parseInt(string);
    		} catch (Exception e) {
    			System.out.println("NumberFormatException");
    		}
    		input.close();
    actually I was using this code to restrict input to digits only. It works fine for digits but for characters it throws NumberFormatException.

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default Re: Parsing Integers

    Well, only digits are numbers, so that sounds about right... How would it translate "car" into a number anyway?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Parsing Integers

    Quote Originally Posted by allaudin View Post
    Java Code:
    Scanner input = new Scanner(System.in);
    		String string = input.next();
    		try {
    			Integer.parseInt(string);
    		} catch (Exception e) {
    			System.out.println("NumberFormatException");
    		}
    		input.close();
    actually I was using this code to restrict input to digits only. It works fine for digits but for characters it throws NumberFormatException.
    1. Don't catch Exception, catch the specific subclass of interest.
    2. Don't print a custom message in a catch(...) { ... } block. print the stack trace.
    Java Code:
    } catch (NumberFormatException e) {
      // System.out.println("NumberFormatException");
      e.printStackTrace();
    }
    3. Don't (attempt to) close() System.in. And when you do have resources that need to be close()d, do that in a finally { ... } block.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: Parsing Integers

    Quote Originally Posted by Toll View Post
    Well, only digits are numbers, so that sounds about right... How would it translate "car" into a number anyway?
    Integer.parseInt("car", 28)?

    kind regards,

    Jos
    PhHein and DarrylBurke like this.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Parsing Integers

    Tell me why it catches exception ??? Toll I didn't get you

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default Re: Parsing Integers

    Quote Originally Posted by allaudin View Post
    Tell me why it catches exception ??? Toll I didn't get you
    Well, you said it catches digits but not characters, right? So Integer.parseInt("23") works, but Integer.parseInt("car") doesn't work? That's because it can't translate "car" into a number (unless you change bases, sneaky Jos :P ). If you want to use Integer.parseInt("one") etc. I think you'd have to do that parsing yourself (unless there's already a translator for that? Don't remember off the top of my head).

  10. #10
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default Re: Parsing Integers

    If you can post what you want to parse and what you want to get out of it, we'd probably be able to help you further.

  11. #11
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

Similar Threads

  1. Variable integers
    By fishfryer in forum Android
    Replies: 6
    Last Post: 05-12-2013, 03:59 PM
  2. Sum of all even integers? Help?
    By psx2514 in forum New To Java
    Replies: 5
    Last Post: 04-03-2013, 07:46 PM
  3. help with randomizing integers
    By tom2zip in forum New To Java
    Replies: 5
    Last Post: 11-28-2011, 12:30 AM
  4. getting integers from a string
    By sehudson in forum New To Java
    Replies: 22
    Last Post: 02-18-2011, 06:34 AM
  5. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 11:29 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
  •