java - 我的isbn检查器有什么错误?

标签 java loops compiler-errors char isbn

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)

最佳答案

该数字太大,无法容纳整数。尝试改用Long

Max Integer is 2147483647.

Max Long is 9223372036854775807L.

关于java - 我的isbn检查器有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34248057/

相关文章:

java - HttpClient 的多线程问题

c - 为什么我调用这个函数8次,结果都是上次的?在 C 中

类型转换 : lvalue required as left operand of assignment

c++ - * token 之前的预期初始化程序

c++ - 在 Debug模式下构建 Qt 5.10 时 MSVC 编译器崩溃

java 。 REST 服务 + Jasper 报告。无法打印标签(或找不到)

java - IBatis sqlmap资源路径在windows中工作而不是在unix中

java - 如何将 Excel 中的图表导出为图形

python - 动态循环遍历Python函数中的函数列表

javascript - 循环中的 If 语句填充表 jQuery