0

I'm new to java and am not up to date on all of the syntax. The end goal is to get the isbn number according to the formula d1 d2 d3 d4 d5 d6 d7 d8 d9 d10, where d10 = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11 To achive this I'm trying to use a for-loop to generate the different number d1, d2 etc from a 9-digit user input, lets say 123456789 for example.

I want to put each of theese digits seperatly into a list or array so that I can call on a list element and the formula.

However I am struggling to get my for-loop working. I've tried to apply the formula on the input without any for loop and just done the for loop manually for each digit, this works, but I figured it would be neater to use a loop.

I'm using eclipse as IDE and don´t seem to get ant actual error, however the result is not what I desired.

Any ideas on how I might tackle this or what I'm doing wrong?

import java.util.ArrayList;
import java.util.Scanner;

public class ISBN1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Integer>[] myArray = new ArrayList[9];
        myArray[0] = new ArrayList<Integer>();
        System.out.print("Enter the first 9 digits of an ISBN as integer: ");
        int isbn = input.nextInt();

        for (int i = 0 ; i < 10; i++ ) { 
            myArray[i].add(isbn / 100000000-10*i);
            int remainingDigits = isbn % 100000000- 10*i;
        }
        for (ArrayList<Integer> mylist: myArray) {
              for (int bar : mylist) {
                System.out.println(bar);
              }
            }
    }


}
Norruas
  • 61
  • 1
  • 9
  • Possible duplicate of [Convert integer to array of digits](https://stackoverflow.com/questions/8033550/convert-integer-to-array-of-digits) – Tom Jun 22 '19 at 17:28
  • Or to get your current approach fixed: [How to get the separate digits of an int number?](//stackoverflow.com/q/3389264) – Tom Jun 22 '19 at 17:28

1 Answers1

0

please try this:

public static void main(final String[] args) {
    final Scanner input = new Scanner(System.in);
    System.out.print("Enter the first 9 digits of an ISBN as integer: ");
    final String isbn = input.nextLine();
    final ArrayList<Integer> myArray = new ArrayList<>(isbn.length());
    for (int i = 0; i < isbn.length(); i++) {
        myArray.add(Integer.valueOf(isbn.substring(i, i + 1)));
    }
    for (final Integer inmylist : myArray) {
        System.out.println(inmylist);
    }
}

Edit if you also want to check the input, you can try this

public static void main(final String[] args) {
    final Scanner input = new Scanner(System.in);
    String isbn = null;
    while (isbn == null) {
        System.out.print("Enter the first 9 digits of an ISBN as integer: ");
        isbn = input.nextLine();
        if (!isbn.matches("\\d{9}")) {
            isbn = null;
        }
    }
    final ArrayList<Integer> myArray = new ArrayList<>(isbn.length());
    for (int i = 0; i < isbn.length(); i++) {
        myArray.add(Integer.valueOf(isbn.substring(i, i + 1)));
    }
    for (final Integer inmylist : myArray) {
        System.out.println(inmylist);
    }
}