ISBN Checker- No Loops

80 views Asked by At

I'm trying to make a ISBN checker but it's not working. An example would be the user inputs 013601267 and return 0136012671. I'm not understanding what the problem is. No loops. Any help would be great.

import java.util.*;
public class ISBN{
public static void main(String[]args){
    Scanner in=new Scanner(System.in);

    System.out.print("Enter 9 digit ISBN");
    //variables//
    int d1=in.nextInt();
    int d2=in.nextInt();
    int d3=in.nextInt();
    int d4=in.nextInt();
    int d5=in.nextInt();
    int d6=in.nextInt();
    int d7=in.nextInt();
    int d8=in.nextInt();
    int d9=in.nextInt();
     int d10=(d1*1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d8*8+d9*9) %11;
    //keyboard


    if (d10==10){
    System.out.print("ISBN"+d1+d2+d3+d4+d5+d6+d7+d8+d9+"X");}
    else if(d10 !=10); {
    System.out.print("ISBN"+d10);}
    }
}
1

There are 1 answers

3
William On

When someone enters their ISBN, are they entering it with spaces in between? nextInt() will retrieve the integers separated by spaces, so it is likely that d1 is receiving the entire nine integers.

If you are entering them one at a time, then it should work.

Either enter the digits with spaces in between them, or each on their own line. Scanner will take care of the rest.

Note:

If you don't want the user to have to enter the digits one-by-one, try taking their input as:

String digits = in.nextLine();

You can reference each digit in that string with digits.charAt(0) etc.

int d1=Integer.parseInt("" + digits.charAt(0));

and so on. This will convert the single character digits.charAt(0) to your integer for the formula.

Hope this helps!