Results 1 to 5 of 5
  1. #1
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default Integer.parseInt() error

    Guys, i am trying to convert a String to integer in Java using Integer.parseInt().

    The problem is the method returns "true" if the String contains only 10 digits or less ("1234567890") and "false" for more than 10 digits.("12345678901"). I want to validate a String for 13 digits and its returning false always. How do i fix it??

    these is my code:

    Java Code:
                      boolean isInt = false;
                      String a = s.getISBN();
    		
    		try {
    			
    			
    			int x = Integer.parseInt(a);
    			isInt = true;
    			System.out.println(" x is an integer: " + x);
    		} catch (NumberFormatException nFE) {
    			System.out.println("not an integer ");
    			
    
    		}
    it returns false when i give a 13 digit number as String. but true for a 10 digit number
    Last edited by niteangell21; 11-16-2010 at 01:40 AM.

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

    Default

    It looks like Java ints don't get that big. You could try making x a long value.

    What are you trying to do? I ask because common operatons on ISBN strings like verifying the digits can be done without forming one large numeric quantity.

  3. #3
    Louis is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    boolean isInt = false;
    String a = s.getISBN();
    long x;
    try {
    if(a.length() < 10)
    {
    x = Long.parseLong(a);
    isInt=true;
    }
    else
    {
    isInt=false;
    }
    } catch (NumberFormatException nFE) {
    System.out.println("not an integer ");
    }

    I think will be like that .

  4. #4
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default

    Long.parseLong(a);
    worked perfect...thnx honey..

  5. #5
    Louis is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

Similar Threads

  1. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 01:50 AM
  2. Integer.parseInt("5.843"); Error
    By Cemi in forum New To Java
    Replies: 3
    Last Post: 04-15-2010, 06:16 PM
  3. [SOLVED] Integer.parseInt() problem
    By eggmanpete in forum New To Java
    Replies: 18
    Last Post: 02-15-2009, 05:25 PM
  4. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 03:31 AM
  5. Problem with Integer.parseInt()
    By Hevonen in forum New To Java
    Replies: 2
    Last Post: 12-14-2008, 04:41 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
  •