【问题标题】:what's the error in my isbn checker?我的 isbn 检查器有什么错误?
【发布时间】:2015-12-13 04:42:16
【问题描述】:
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)

【问题讨论】:

    标签: java loops compiler-errors char isbn


    【解决方案1】:

    这个数字太大而无法放入整数。尝试改用Long..

    最大整数为 2147483647。

    最大长度为 9223372036854775807L。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 2020-08-23
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      相关资源
      最近更新 更多