1

I am trying make a Digits method that stores digit of the num that is passed in in an ArrayList digitList. Instead of dividing by 10 and getting the modulus, I am converting the integer num to a string and trying to store each digit using charAt. But I must convert it back to integer to store it in the ArrayList and struggling on how to do it.

Thank you very much!

 class Digits{
 
 ArrayList<Integer>digitList;

Digits(int num) {
    String StringN = Integer.toString(num);
    int index = 0;
    new ArrayList<Integer>(); 
    for(int i=0;i<StringN.length();i++) {

        // convert the charAt(i) to integer here?

        digitList.add(index, StringN.charAt(i)); 
  
        index++;
    }
    


      }
 }

 public class digitListclass {

public static void main(String[] args) {
    
    
    

}

 }
Selina
  • 11
  • 3

3 Answers3

1

tl;dr

Use Unicode code points rather than char, as a best practice.

"1234567890"
        .codePoints()
        .map( 
            ( int codePoint ) -> Integer.parseInt( Character.toString( codePoint ) ) 
        )
        .boxed()
        .toList()

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

char obsolete

The char type is obsolete, unable to represent even half of the characters defined in Unicode. While this type would work for strictly Arabic numerals, we should make a habit of using code point integer numbers rather than char type values.

IntStream of code points

The easiest way to work with text as code point numbers is to call String#codePoints to get an IntStream, a stream of int primitive values. For digit 1, we get code point 49, for digit 2 code point 50, and so on, with 0 being assigned to code point 48.

For each int representing a code point number, convert to the character assigned to that number as a String object. So 49 becomes "1", 50 becomes "2", and so on.

Then parse each digit-as-string into an int value for the digit value. Throws a NumberFormatException if the string is not a parsable integer.

By calling .boxed, we convert each int primitive for the digit to an Integer object.

Lastly, we collect the Integer objects representing each digit into an unmodifiable List. The Stream#toList method is new in Java 16. For earlier Java, use .collect( Collectors.toList() ).

List < Integer > digits =
        "1234567890"
                .codePoints()
                .map( codePoint -> Integer.parseInt( Character.toString( codePoint ) ) )
                .boxed()
                .toList();

Here is a full working example class with main method.

package work.basil.demo.text;

import java.util.List;

public class App2
{
    public static void main ( String[] args )
    {
        List < Integer > digits =
                "1234567890"
                        .codePoints()
                        .map( codePoint -> Integer.parseInt( Character.toString( codePoint ) ) )
                        .boxed()
                        .toList();

        System.out.println( "digits = " + digits );
    }
}

When run.

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

This might help you -

import java.util.ArrayList;
import java.util.List;

public class Test {
  List<Integer> toList(int num) {
    String stringNumber = Integer.toString(num);
    List<Integer> result = new ArrayList<Integer>(stringNumber.length());
    for(int i = 0; i < stringNumber.length(); i ++) {
      result.add(Integer.valueOf(stringNumber.substring(i, i + 1)));
    }
    return result;
  }

  public static void main(String[] args) {
    System.out.println(new Test().toList(1234567890));
  }
}
Sanju Thomas
  • 181
  • 2
  • 10
0

I think there are some problems in your code:

  1. Your way of initializing digitList is not correct since it's not being assigned to any variable. You should rather do:
this.digitList = new ArrayList<>();
  1. I think you have a redundant index variable for your loop as it doesn't seem to be getting used anywhere. You should remove it and just use i.
  2. You should keep your class variables private by default and final if they are not supposed to be changed. Use getters and setters for accessing variable.

I modified your code like this:

class Digits {
    private final ArrayList<Integer> digitList;

    public Digits(int num) {
        this.digitList = new ArrayList<>();
        initDigitList(num);
    }

    private void initDigitList(int num) {
        String stringN = Integer.toString(num);
        for (int i = 0; i < stringN.length(); i++) {
            // convert the charAt(i) to integer here?
            digitList.add(i, stringN.charAt(i) - '0');
        }
    }

    public List<Integer> getDigitList() {
        return this.digitList;
    }
}

I tested it with this code and it seems to work:

Digits first = new Digits(512);
Digits second = new Digits(1024);

// Prints First Digit: [5, 1, 2]
System.out.println("First Digit: " + first.getDigitList().toString());
// Prints Second Digit: [1, 0, 2, 4]
System.out.println("Second Digit: " + second.getDigitList().toString());
Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40