-1

I am trying to create a method that with open a file then read the file line by line no matter the length of file. Each line is split into words and punctuation (excluding the apostrophe) and stored in an ArrayList of Strings. These ArrayLists representing the line are stored in an ArrayList of ArrayLists of Strings.

For example if the file contains.

Hello World!
Hi, hello.

Then the first array list would contain the strings: "Hello", "World", "!" The second array list would contain the strings: "Hi", ",", "hello", "."

So far I have the following and am not sure if I am going in the right direction.

public static void readInputFile(String fileName, ArrayList<ArrayList<String>> fileByLine)
    throws IOException {

    File userFile = new File(fileName);

    try {
        if (userFile.exists()) {
            Scanner fileScanner = new Scanner(userFile);
            do {
                do {
                    fileByLine.add(fileScanner.next());
                } while (fileScanner.next() != null);
            } while (fileScanner.hasNext());
        }
    } catch (Exception e) {
        System.out.println("Exception: File '" + fileName + "' not found.");
    }
}
Brandon
  • 1
  • 2
  • 1
    Please [take the tour](http://stackoverflow.com/tour) to see how the site works and what questions are on topic here, and [edit] your question accordingly. See also: [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Joe C Nov 29 '17 at 06:35

1 Answers1

0

Below code works for me, You can try..

public class ListOFList {

    public static ArrayList<String> stringspecial(String strLine) {
        String str = strLine;
         String arr[]=str.split(" ");

         ArrayList<String> list = new ArrayList<String>();

          for(int i=0;i<arr.length;i++) {
              Pattern pattern = Pattern.compile("[a-zA-Z0-9]*");
              Matcher matcher = pattern.matcher(arr[i]);


          if (!matcher.matches()) {
               char[] ch = arr[i].toCharArray();
               String substring="";

               for(int j=0;j<ch.length;j++) {


                   if(!(Character.isLetter(ch[j]))) {
                       String special = Character.toString(ch[j]);
                       list.add(special);
                   }else {
                       substring=substring+ch[j];
                       if(!(Character.isLetter(ch[j+1]))) {
                           list.add(substring);
                   }

                   }
               }


          } else {
              list.add(arr[i]);

          }

    }
          return list;
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        FileInputStream fi = new FileInputStream("<Path of the Input Text File>");
        BufferedReader br = new BufferedReader(new InputStreamReader(fi));
        String strLine;
        ArrayList<ArrayList<String>> listoflists = new ArrayList<ArrayList<String>>();
        ArrayList<String> list = new ArrayList<String>();
        while ((strLine = br.readLine()) != null)   {
            list=stringspecial(strLine);
            listoflists.add(new ArrayList(list));
            list.clear();    
              }
        for(int x = 0; x < listoflists.size(); x++)
        {

                System.out.print(listoflists.get(x));
                System.out.print(" ");

        }

        br.close();

                }

        }

INPUT:

Hello World! Hi, hello. Hello World! Hi, hello. Hello World! Hi, hello.

OUTPUT:

[Hello, World, !] [Hi, ,, hello, .] [Hello, World, !] [Hi, ,, hello, .] [Hello, World, !] [Hi, ,, hello, .]

Dora
  • 191
  • 8