Reply
Old 06-25-2014, 02:06 AM   #1 (permalink)
 
Status: Junior Member
Posts: 23



Default can someone fix my java code it's about isbn 13 code change, i keep getting error messages

output is like this
Enter the first 12 digits of an ISBN-13 as a string:9780132130806.
The ISBN-13 number is 9780132130806.
public class isbnchecker {
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.println("input an isbn");
String isbn = scan.nextLine();
isbnchecker main = new isbnchecker();
System.out.println(isbn + main.checkISBN(isbn));
}

private boolean checkISBN (String isbn){
char[] isbnChar = isbn.toCharArray();
//check if the length is 13
if (isbnChar.length != 13)
return false;
//check if only digits

for(int i=0;i<isbnChar.length;i++){
if (!Character.isDigit(isbnChar[i]))
return false;
}
//check if the checknumber is correct
int sum = 0;



for(int i=0;i<(isbnChar.length-1);i=i+2){
sum = sum + Character.getNumericValue(isbnChar[i]);
}
for (int i=1;i<isbnChar.length;i=i+2){
sum = sum + (Character.getNumericValue(isbnChar[i])*3);
}
int CheckDigit = 10-sum%10;
int LastDigit = Character.getNumericValue(isbnChar[isbnChar.length]);
if (CheckDigit != LastDigit)
return false;
return true;}}
Tyrone is offline   Reply With Quote
Old 06-25-2014, 02:06 AM   #2 (permalink)
 
Status: Senior Member
Posts: 1,070



Default can someone fix my java code it's about isbn 13 code change, i keep getting error messages

You use isbnChar[isbnChar.length] to get the last character in the array isbnChar. Array index values are zero-based, so that should be isbnChar[isbnChar.length-1].

The error message says that in the exception name ArrayIndexOutOfBoundsException.
husoski is offline   Reply With Quote
more..

Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
java code help? I can't seem to get this ISBN 13 right, 10-(d1 +3d2 +d3 +3d4 +d5 +3d6 +d7 +3d8 +d9 +3d10 +d11 +3d12) % 10, the formula? Tyrone Coding 2 06-25-2014 06:06 AM
help me fix my java code?im suppose to check ISBN 13 with my code.? Jack Coding 0 06-24-2014 10:06 PM
Why do I keep getting error messages in this code.? GMONEY Coding 0 02-09-2013 03:05 AM
JAVA Code Help: ISBN Checker Shirapsynge Coding 0 09-15-2011 10:06 PM
I receive error messages for code.. frontru Coding 0 07-28-2011 10:06 AM