-1

I am still new to Java, this question like: An ISBN-10 (International Standard Book Number) consists of 10 digits: d1d2d3d4d5d6d7d8d9d10. The last digit, d10, is a checksum, which is calculated from the other nine digits using the following formula: (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11 If the checksum is 10, the last digit is denoted as X according to the ISBN-10 convention. Write a program that prompts the user to enter the first 9 digits and displays the 10-digit ISBN (including leading zeros). Your program should read the input as an integer.

See my code below: It is working but the result is not correct!

public static Scanner input;

    public static void main(String[] args)
    {
        input = new Scanner(System.in);
        System.out.print("Enter first 9 digit numbers: ");
        int[] arr = new int[9];
        int checkSum = 0;

        for (int i = 0 ; i < arr.length; i++)
        {
            arr[i] = input.nextInt();
            checkSum = (arr[i] * i) % 11;   
        }
        System.out.print("The ISBN-10 Number is " );
        for(int j = 0 ; j < arr.length; j++)
        {
            System.out.print(arr[j]);
        }

        if(checkSum == 10)
        {
            System.out.println("x");
        } 
        else
        {
            System.out.println(checkSum);
        }

I just want to use loop, make my method works. , i know how to use method without loop.

Twocold
  • 11
  • 3
  • Well, Java uses `0-based` indexing for arrays. So, you if you declare and array as `int[] arr = new int[9];`. You have 9 elements which can be indexed using `0 to 8`. In example B, you are using `1-9`, here `9` means accessing the `10th` element which does not exist. – vishal-wadhwa Dec 23 '17 at 05:35

4 Answers4

1

well. for JAVA an array index start from 0. and Max index is length - 1. if the index is not within this range. an arrayoutofbounds exception will be thrown

Hash Jang
  • 599
  • 3
  • 11
1

The problem is not that you are doing int i = 1 instead of int i = 0. The problem is that you changed i < arr.length; to i <= arr.length;. Since arr.length is 9, your code is trying to refer to arr[9] but arr only has elements arr[0] through arr[8].

Josh Withee
  • 9,922
  • 3
  • 44
  • 62
0
int[] arr = new int[9];

This means the array has 9 slots. And the numbering of those slots starts from 0. Thus,

  • arr[0] is the first slot
  • arr[1] is the second slot

The last slot would be arr[8]. But in the following code you are iterating till i is equal to 9 which does not exist.

for (int i = 1 ; i <= arr.length; i++)
    {
        arr[i] = input.nextInt();
        checkSum = (arr[i] * (i+1)) % 11;   
    }

This results in the ArrayIndexOutOfBoundsException. Change for (int i = 1 ; i <= arr.length; i++) to for (int i = 0 ; i < arr.length; i++)

Ishan Thilina Somasiri
  • 1,179
  • 1
  • 12
  • 24
0

First, review how we use arrays...

int[] I = new int[3];

The above creates an array with 3 spots in it. After instantiating the array, each element is based off of a 0-index. You only have I[0], I[1], and I[2] available (note this is three numbers) and trying to access I[3] will throw the error that you encountered in your program above: ArrayIndexOutOfBoundsException.

That being said, your loop is trying to access arr[9]. The highest index you have in the array is arr[8], because despite the array having 9 elements in it, it's 0-indexed.

Assuming you MUST have i starting from 1 for a homework assignment or something, change your code to:

public static Scanner input;

    public static void main(String[] args)
    {
        input = new Scanner(System.in);
        System.out.print("Enter first 9 digit numbers: ");
        int[] arr = new int[9];
        int checkSum = 0;

        for (int i = 1 ; i <= arr.length; i++)
        {
            arr[i-1] = input.nextInt();
            checkSum = (arr[i-1] * i) % 11;   
        }
        System.out.print("The ISBN-10 Number is " );
        for(int j = 1 ; j <= arr.length; j++)
        {
            System.out.print(arr[j-1]);
        }

        if(checkSum == 10)
        {
            System.out.println("x");
        } 
        else
        {
            System.out.println(checkSum);
        }
Shane Sepac
  • 806
  • 10
  • 20