-2

I have an ArrayList named lines. This contains a text file of numerous 0's and 1's split by commas. My issue is trying to convert this arraylist into an integer array. I have tried the following, but am still struggling. What am I missing?

    int[][] result = new int [lines.size()][];
    for (int i = 0; i<result.length; i++) {
        result.add(Integer.valueOf(lines.getString(i)));
    }

And

    int[][] result = new int [lines.size()][];
    for(int i = 0; i<result.length; i++) {
        result[i] = Integer.parseInt(lines.get(i)); 
    }
JoCode
  • 1
  • Possible duplicate of [Converting 'ArrayList to 'String\[\]' in Java](https://stackoverflow.com/questions/4042434/converting-arrayliststring-to-string-in-java) – alexanders916 Apr 29 '18 at 15:53
  • @alexanders916 My guess is that it is because list "*contains a text file of numerous 0's and 1's **split by commas***" so from list containing values like "1,0,0"`, `"0,0,1"` OP probably want to get 2D array like `[[1, 0, 0], [0, 0, 1]]`. But yes, question should be clarified to avoid guessing. – Pshemo Apr 29 '18 at 15:54
  • Can you provide an example of what your lines ArrayList contains ? – Gautam Apr 29 '18 at 15:57
  • Please be more specific with where you are struggling. Preferably with an error message or an incorrect result. – Joe C Apr 29 '18 at 16:02
  • replace `int[][] result = new int [lines.size()][];` with `int[] result = new int [lines.size()];` at the second code block, there is not an `add` function for `array` – shahaf Apr 29 '18 at 16:04
  • OP, in case this is entirely your project (i.e. not an assignment), and the numbers are indeed only 1 digit (e.g. 0 and 1), you should consider not using comma separation at all. It seems wasteful and just complicates things. Except if you have a good reason, of course. – Dreamspace President Apr 29 '18 at 17:52

2 Answers2

0

If you really just want to parse each line as a single number:

int[] result = new int [lines.size()];
for(int i = 0; i<result.length; i++) {
    result[i] = Integer.parseInt(lines.get(i)); 
}

If you want to split each line by commas and have a 2D array:

int[][] result = new int [lines.size()][];
for(int i = 0; i<result.length; i++) {
    String[] nums = lines.get(i).split(",");
    result[i] = new int[nums.length];
    for (int j=0; j<nums.length; j++){
      result[i][j] = Integer.parseInt(nums[j]);
    }
}
  • I had to add a command to split the line by commas.
  • Had to create each of the 'lower dimensional' arrays.

    result[i] = new int[nums.length];

Without this statement, each slot in result is pointing to null, instead of an actual int array.

Carl Shiles
  • 434
  • 3
  • 15
0

if you only want the 0's and 1's in your new array, then you need to put an if statement to make sure that commas are not included in your array.

this is the code:

enter code here
    int[] result = new int [lines.size()];
    for(int i = 0; i<result.length; i++) {
        if(!lines.get(i).equals(","))
            result[i] = Integer.parseInt(lines.get(i)); 

but if you want to include the commas in an integer array then they will be converted into ASCII which will represent the comma as an integer of 44. here is the code that includes the commas:

enter code here

    int[] result = new int [lines.size()];
    for(int i = 0; i<result.length; i++) {
        if(!lines.get(i).equals(","))
            result[i] = Integer.parseInt(lines.get(i)); 
        else
            result[i] = lines.get(i).charAt(0); 
aziz aon
  • 143
  • 2
  • 8