Search code examples
javaarraylisttrim

Removing all spaces in an ArrayList. Java


I am taking input from a .txt file and storing it into an ArrayList. I need to get rid of all the spaces so I can parse the ArrayList to an <Integer>. I am going to post commented out sections so you can see what i have tried to use to do this. Thank you. If you can give me somewhere to look that would be great.

import java.io.*;
import java.util.*;

public class LabWriterReadre {

  public static void main(String [] args){
       BufferedReader br = null;
       BufferedWriter bw = null;
      // fileWriter = new FileWriter("output.txt",false);
       ArrayList<String> storage = new ArrayList<String>();
       ArrayList<String> convertToInt = new ArrayList<String>();
       ArrayList<Integer> matrix = new ArrayList<Integer>();
         String strLine = "";
        int row1,col1,row2,col2; 

        try {
            br = new BufferedReader( new FileReader("C:/Users/jeremiahlukus/Desktop/New folder/jj.txt"));
            while( (strLine = br.readLine()) != null)
            {
                System.out.println(strLine);
                storage.add(strLine);             
            }
        } catch (FileNotFoundException e) {
            System.err.println("Unable to find the file: fileName");
        } catch (IOException e) {
            System.err.println("Unable to read the file: fileName");
        }


        /*
        String[] trimmedArray = new String[string.size()];
        for (int i = 0; i < string.size(); i++)
        {
            trimmedArray[i] = string[i].trim();
            string.removeAll(Arrays.asList(null,""));
        }
        */
        //string.removeAll(Collections.singleton(""));
       // string.removeAll(Arrays.asList(null,"")

      for (String str : storage)
      {
         if (//str != " ")
             !str.isEmpty())
         {
          convertToInt.add(str);
          //convertToInt.trim();

         }
     }

System.out.println(convertToInt);
}

 /* 

       row1 = Integer.parseInt(convertToInt.get(0));
       col1 = Integer.parseInt(convertToInt.get(1));
       row2 = Integer.parseInt(convertToInt.get(2));
       col2 = Integer.parseInt(convertToInt.get(3));

       int[][] matrix1=new int[row1][col1];
       int[][] matrix2=new int[row2][col2];

     */  

      //  System.out.println(row1);


        /*

This is the .txt file I am reading, _ means space

3___________3
3_4

1 ______2 3 
4_5 _6
7 _8 _9

1 _2_ 3_ 4
5 _6 _7 _8
9_ 10_ 11_ 12

This is the ArrayList printed out

[3______3,_3 _4,__ 1_______2_ 3 , ___4 _5 _6, 7_ 8 _9, 1_ 2_ 3_ 4, 5 _6_ 7_ 8, 9 _10_ 11_ 12]

This is what I want to happen

[3,3,3,4,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,10,11,12]

Solution

  • I may be wrong but it looks like your code could be solved and simplified a little with something like

    Scanner input = new Scanner(new File("C:/Users/jeremiahlukus/Desktop/New folder/jj.txt"));
    List<Integer> numbers = new ArrayList<>();
    while(input.hasNextInt()){
        numbers.add(input.nextInt());
    }
    

    I am assuming here that your file contains only numbers separated by whitespaces (which includes also line separators).