0

I'm new to Java, so apologies if any of this is unclear - I want to convert a list of four-digit integers into a list of integer arrays - i.e if I have an integer that is 4567, I want to convert that into an array of four separate integers [4, 5, 6, 7]. So I want to convert each line/index of the list into it's own array (rather than converting the entire list into an array). I'm currently reading a file that has four-digit integers (each on a new line) and adding them to a list, which is then returning that list - any ideas on how could I code it so that it returns a list of integer arrays instead?

    public List <Integer> loadGuesses (String fileName){

    List<Integer> loadGuessList = new ArrayList<>();

    try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
        String line;
        while ((line = reader.readLine()) != null) {
            loadGuessList.add(Integer.parseInt(line));
        }
    } catch (FileNotFoundException f){
        System.out.println("File not found - please re-enter with correct file name.");
        getPlayerFile();
    } catch (IOException e){
        System.out.println("Incorrect file name - Please re-enter with correct file name.");
        getPlayerFile();
    }
    return loadGuessList;

}
Logan
  • 45
  • 1
  • 7

6 Answers6

1

Change your type of loadGuessList to List<int[]>.

After reading the string, you can iterate through the characters and convert each to an int.

  List<int[]> loadGuessList = new ArrayList<>();

  while ((line = reader.readLine()) != null) {
    int [] digits = new int[4];
    for (int i = 0; i < line.length(); i++) { //Assuming line.length() will be 4
        digits[i] = line.charAt(i) - '0';
    }
    loadGuessList.add(digits);
}

Reference: How can I convert a char to int in Java?

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
1

To convert a string with a number into a array of integers with the digits of the text, use this method:

public static int[] toDigits(String text) {
    int[] digits = new int[text.length()];
    for (int i = 0; i < text.length(); i++)
        digits[i] = Character.digit(text.charAt(i), 10);
    return digits;
}

Test

System.out.println(Arrays.toString(toDigits("4567")));
System.out.println(Arrays.toString(toDigits("0014789632500")));
System.out.println(Arrays.toString(toDigits("123FOO")));

Output

[4, 5, 6, 7]
[0, 0, 1, 4, 7, 8, 9, 6, 3, 2, 5, 0, 0]
[1, 2, 3, -1, -1, -1]
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

you can try this:

public static void main(String[] args) throws Exception {
    List<Integer> list = new ArrayList<Integer>();

    BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
    String line = null;
    while((line = reader.readLine()) != null ) {
        char[] tmp = line.toCharArray();
        for(int i=0; i<tmp.length; i++) {
            list.add(Character. getNumericValue(tmp[i]));
        }

    }
    // TODO release resources
}
nnzhang
  • 21
  • 1
0

List class already provides a convenient method ( toArray ) for converting itself to an array. You can use the below snippet as a reference for your problem.


    public Integer[] loadGuesses(String fileName) {
            List loadGuessList = new ArrayList();

            /**
             * all of your code.
             */

            return loadGuessList.toArray(new Integer[loadGuessList.size()]);
    }

Muthukrishnan
  • 2,047
  • 2
  • 16
  • 16
0

You can just use the toCharArray method of the String class:

loadGuessList.add(line.toCharArray())

Now loadGuessList is a List<Chat[]>.

Michael
  • 2,443
  • 1
  • 21
  • 21
0

I highly recommend the the Guava which is produced by Google to handle the string or collections if you need it. maybe it is not suit for this topic,but you will get benifits from it in your later projects.