-1
import java.util.Scanner;

// 変数を宣言する

public class ISBNChecker {
public static void main(String [] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter a 13 digit book number:");
    String book = keyboard.nextLine(); 
    int isbn = Integer.parseInt(book);

// isbn 番号の長さをチェック

    if (book.length() != 13) {
        System.out.println("ILLEGAL ISBN NUMBER");
    }

// 1,3,5,7,9,11 ごとの数の倍数、および 13 番目の数が 3 で乗算され、他のすべての数が 1 で乗算されているかどうかを確認し、それらを合計します

    for(int i = 0; i < book.length(); i++){
        char c = book.charAt(i); //get the current char
        int value = c - '0'; //subtract '0' from the char
        isbn += value * (i+1);

//the rest is to see if total of isbn is dividable by 10 to have no remains

    }
    if ((isbn % 10) != 0) {
        System.out.println("Illegal ISBN number");
    } else {
        System.out.println("It's an ISBN number");
    }
}

}

//the problem with this code is that it won't work and I am pretty sure I messed up the format. Please help me check. 
// when I process it. this error shows up :

Exception in thread "main" java.lang.NumberFormatException: For input string: "1234567898765"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:583)
    at java.lang.Integer.parseInt(Integer.java:615)
    at ISBNChecker.main(ISBNChecker.java:8)
4

1 に答える 1

2

その数は大きすぎて整数に収まりません。代わりに使用してみてくださいLong..

最大整数は 2147483647 です。

最大長は 9223372036854775807L です。

于 2015-12-13T04:47:44.760 に答える