简体   繁体   中英

(Java) Parsing a String into a 2D Array of Integers

It's my first time posting here, so please be gentle.

I've been given a plain text file that has an accumulation of numbers on different lines. (10x10 grid)

0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789

I've managed to load it into my Java application and print it out using..

System.out.println(stringBuffer.toString());

This prints out the file to the command line with no issues.

But I can't seem to parse it through as a integer in a 2D array?

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

How can I achieve this?

Thank you.

try

import java.util.Arrays;
public class test {
    public static void main(String[] args) throws FileNotFoundException {
        StringBuffer stringBuffer = new StringBuffer("0123456789 \n 0123456789 \n 0123456789 \n 0123456789 \n 0123456789 \n 0123456789 \n 0123456789\n 0123456789 \n 0123456789 \n 0123456789 \n 0123456789 \n 0123456789\n0123456789\n0123456789");
        String str[]= stringBuffer.toString().split("\n");
        int[][] arr = new int[str.length][];

        for(int i=0;i<str.length;i++){
            String currentLine=str[i].trim();
            int[] temp = new int[currentLine.length()];         
            for(int j=0;j<currentLine.length();j++){
                temp[j] =  Character.getNumericValue(currentLine.charAt(j));
            }
            arr[i]=temp;

        }
        System.out.println(Arrays.deepToString(arr));
    }

}

Use String.toCharArray() method:

// after reading a line into 'numString'

char [] charList = numString.toCharArray();
int [] intList = new int [charList.length];

for(int i = 0; i < intList.length; i++) {
    intList[i] = charList[i] - '0';

// now you have the ints from the line into intList
public int[][] to2D(String value) {
    String[] linesArray = value.split("\n");
    int[][] array2d = new int[linesArray.length][];
    for (int i = 0; i < linesArray.length; i++) {
        char[] lineArray = linesArray[i].toCharArray();
        int[] array1d = new int[lineArray.length];
        for (int j = 0; j < lineArray.length; j++) {
            array1d[j] = Character.getNumericValue(lineArray[j]);
        }
        array2d[i] = array1d; 
    }   
    return array2d;
}

I prefer to work with Collections though (ArrayList in that case):

public ArrayList<ArrayList<Integer>> to2DList(String value) {
    ArrayList<ArrayList<Integer>> arrayList2d = new ArrayList<ArrayList<Integer>>();
    String[] lines = value.split("\n");
    for (String line : lines) {
        ArrayList<Integer> arrayList1d = new ArrayList<Integer>();
        for (char digit : line.toCharArray()) {
            arrayList1d.add(Character.getNumericValue(digit));
        }
        arrayList2d.add(arrayList1d);
    }   
    return arrayList2d;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM