You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ISBN-13校验位计算时字符串转Long拆分数字报错求解决

Hey there! Let's break down the issues in your code that's causing that error, and fix them step by step.

1. Uninitialized Array (NullPointerException)

You declared int[] digits; but never created an actual array instance. Right now, digits is null, so trying to assign digits[i] = ... will throw a NullPointerException. You need to initialize it with a fixed size (since we're dealing with 12 digits):

int[] digits = new int[12];

2. Type Mismatch Error

temp is a Long type, so temp % 10 returns a long value. But digits is an int[], which can't hold long values directly. You need to cast the result to int:

digits[i] = (int) (temp % 10);

3. Incorrect Loop Range

Your loop runs from i=0 to i<=12 (13 iterations total), but we only have 12 digits from the input. This will cause an ArrayIndexOutOfBoundsException once i reaches 12 (since our array is size 12, indexes go from 0 to 11). Change the loop condition to:

for(int i = 0; i < 12; i++) {

4. Logic Error: Reversed Digits

When you convert the ISBN string to a Long and extract digits using temp%10, you're getting the digits in reverse order (starting from the last character of the input string). This means your check digit calculation uses the wrong order of digits, leading to an incorrect result. The ISBN-13 check digit requires processing digits from left to right, so extracting them directly from the string fixes this issue.

5. Simplified, Error-Free Approach

Instead of converting the string to a Long (which adds unnecessary complexity), you can directly access each character in the input string to get its numeric value. This avoids all type conversion headaches and reversed-digit logic.

Here's the revised code with all fixes:

import java.util.Scanner;

public class Exercise05_47 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] digits = new int[12]; // Initialize array properly
        int d13 = 0;

        System.out.println("Enter the first 12 digits of an ISBN-13 as a string: ");
        String ISBN = sc.nextLine();

        if (ISBN.length() != 12) {
            System.out.println(ISBN + " is an invalid input");
            return;
        }

        // Extract digits directly from the string (left to right)
        for (int i = 0; i < 12; i++) {
            // Convert char to numeric digit by subtracting ASCII value of '0'
            digits[i] = ISBN.charAt(i) - '0';
        }

        // Calculate the check digit using ISBN-13 rules
        int sum = 0;
        for (int i = 0; i < 12; i++) {
            sum += (i % 2 == 0) ? digits[i] : 3 * digits[i];
        }

        d13 = 10 - (sum % 10);
        if (d13 == 10) {
            d13 = 0;
        }

        System.out.println("The ISBN-13 number is " + ISBN + d13);
    }
}

This code fixes all syntax errors, corrects the digit order logic, and makes the calculation more readable.

内容的提问来源于stack exchange,提问作者anyelengyuezjn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:40:32