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



Default 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

the output should be:
Enter the first 12 digits of an ISBN-13 as a string:978013213080
The ISBN-13 number is 9780132130806
import java.util.Scanner;
public class IBN13 {
public static void main(String[] args){
int d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12;
Scanner input = new Scanner(System.in);
String line = input.nextLine();
int[] digit = new int[line.length()];
for(int i = 0; i < digit.length; ++i)
digit[i] = line.charAt(i) - '0';

System.out.print("Enter the 13 digits of the ISBN: ");

int d13 = input.nextInt();

d13 = 10 - ((d1 *1 ) + (d2*3) + (d3*1) + (d4*3) + (d5*1) + (d6*3) +
(d7*1) + (d8*3) + (d9*1) + (d10 * 3) + (d11*1) + (3* d12)) % 10);

if (d13 == 13){


System.out.print ( "ISBN is:" + d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 +
d9 + "X");



}
else {

System.out.print ( "ISBN is:" + d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 +
d9 + d10);

}

}

}
Tyrone is offline   Reply With Quote
Old 06-25-2014, 07:06 AM   #2 (permalink)
 
Status: Senior Member
Posts: 744



Default 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

You defined variables d1, d2, d3... but you didn't assign values to any of them.
Bill is offline   Reply With Quote
more..
Old 06-25-2014, 07:06 AM   #3 (permalink)
 
Status: Senior Member
Posts: 2,787



Default 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

Maybe you want to replace d1 with digit [0], and d2 with digit [1].

You are never initializing d1, d2, etc.

But since you seem to know how to use an array, you wouldn't want to use d1, d2, etc.

========= NEW CODE ===========
import java.util.Scanner;
public class IBN13 {
public static void main (String [] args) {
Scanner input= new Scanner (System.in);
System.out.println ("input isbn12");
String isbn = input.nextLine ();
System.out.println ("ISBN is " + makeISBN (isbn));
}

static String makeISBN (String isbn) {
char[] isbnChar = isbn.toCharArray ();

if (isbnChar.length != 12) return "to short";
for(int i=0; i<isbnChar.length; i++) {
if (!Character.isDigit(isbnChar[i])) return "not all digits";
}

int sum = 0;
for(int i=0; i<(isbnChar.length-1); i+=2)
sum += Character.getNumericValue (isbnChar [i]);
for (int i=1; i<isbnChar.length; i=i+2)
sum += (Character.getNumericValue (isbnChar [i])*3);
int CheckDigit = 10 - sum%10;
return isbn + CheckDigit;
}
}
EddieJ is offline   Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
can someone fix my java code? it's about ISBN 13 code change, I keep getting error messages? Tyrone Coding 1 06-25-2014 03:06 AM
help me fix my java code?im suppose to check ISBN 13 with my code.? Jack Coding 0 06-24-2014 11:06 PM
java program(how i can write java code for math formula using scanner)? is anyone there to help me? Ally Coding 1 01-30-2013 03:06 AM
java programming isbn checker? Unknown_User Programming 0 02-19-2012 07:11 AM
JAVA Code Help: ISBN Checker Shirapsynge Coding 0 09-15-2011 11:06 PM