Results 1 to 8 of 8
  1. #1
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default Number formatting

    OK. Here's my problem. I'm making a program that takes the first twelve digits of an ISBN, calculates the last digit, and then adds hyphens. I realize that the placement of hyphens varies depending on the particular ISBN, but my teacher is an idiot. I haven't finished the program yet, so a couple parts might not really make sense. I don't get any build errors, but after I enter the first 12 digits, it generates this error message:
    Java Code:
    Exception in thread "main" java.lang.NumberFormatException: For input string: "123553845931"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
        at java.lang.Integer.parseInt(Integer.java:461)
        at java.lang.Integer.parseInt(Integer.java:499)
        at Checksum.<init>(ISBN.java:39)
        at ISBN.main(ISBN.java:26)
    Here's my code:
    Java Code:
    import java.util.Scanner;
    import java.lang.String;
    import java.lang.Integer;
    
    public class ISBN {
        
        public static void main(String[] args) {
        	String isbn;
        	Checksum nisbn;
        	Scanner input = new Scanner(System.in);
        	System.out.print("Enter the first 12 digits of an ISBN as a string: ");
        		isbn = input.next();
        	while (isbn.length() != 12) {
        		System.out.println("That ISBN was not 12 digits long. Please enter a new one: ");
        			isbn = input.next();
        	}
        	nisbn = new Checksum(isbn);
        }
    }
    class Checksum {
    	private int nisbn;
    	int checksum;
    	int num;
    	public Checksum(String isbn) {
    		nisbn = Integer.parseInt(isbn);
    		checksum = 0;
    		num = 0;
    	}
    	public int Calculator() {
    		for (int i = 1; i <= 6; i++){
    			num = nisbn % 10;
    			checksum += num;
    			nisbn /= 10;
    			num = (nisbn % 10) * 3;
    			checksum += num;
    			nisbn /= 10;
    		}
    		return(checksum);
    	}
    		
    }

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

    Default

    The value 123553845931 doesn't fit in a 32 bit int; better try a long.

    kind regards,

    Jos

  3. #3
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Oh. I feel stupid.

  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

    Default

    That's means on this line..

    Java Code:
    nisbn = Integer.parseInt(isbn);
    int type can store values from -2,147,483,648 to 2,147,483,647 and the number you tried is 123,553,845,931 which is not fit with it.

  5. #5
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    I knew what he meant, I was just feeling stupid for missing something that obvious.

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

    Default

    I wonder then why you didn't realize just looking at the exception, java.lang.NumberFormatException:

  7. #7
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    That's exactly what I was saying, that this was ridiculously stupid of me to post this without thinking first.

  8. #8
    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. Finding a number in array close to another number
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 02-15-2010, 01:37 AM
  2. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 11:42 PM
  3. Formatting isbn number with Math.random()
    By dns77x7 in forum New To Java
    Replies: 11
    Last Post: 09-21-2008, 07:02 PM
  4. Formatting a number to currency
    By Java Tip in forum java.text
    Replies: 0
    Last Post: 04-16-2008, 11:59 PM
  5. Correct Number formatting
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:59 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
  •