简体   繁体   中英

How do I add lines (read from a .txt) separated by AN ENTER KEY (in .txt) into separate elements of string arrayList?

I'm reading .txt file into my program and am adding lines of the .txt into a String arrayList. How do I add lines DELINEATED BY AN ENTER KEY (in .txt) into separate elements of the arrayList? Right now if I had the following written in text:

this is a test

test

test test

It would output:

this is a testtesttest test

What I want it to do is read things on a per line basis, and put it into different elements of the stringArrayList. So I want "this is a test" to be an element, and "test" , and then finally "test test" .

My code is really ugly, but right now all I want to do is get it to work for my purpose. My first purpose is getting to read a .txt by line. My second purpose is going to be parsing an element for a particular substring (a URL), connecting that URL to the internet, and then comparing a part of that page source of the webpage (parsing for a particular keyword) to the line ABOVE the substring I desire. But that's a question for another time :^)

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


public class Test {
    public static void main(String [] args) {

        // The name of the file to open.
        String fileName = "test.txt";
        List<String> listA = new ArrayList<String>();

        // This will reference one line at a time
        String line = null;

        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {                
                System.out.println(line);
                listA.add(line);
                //*** THIS IS WHERE THE MAGIC HAPPENS ***\\ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            }   

            // Always close files.
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open da file ofheee hah. '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                  
            // Or we could just do this: 
            // ex.printStackTrace();
        }

        System.out.println();
        System.out.println("array FOr loop thingy incoming:");
        System.out.println();


        for (int i = 0; i < listA.size(); i++) {
            System.out.print((listA.get(i)).toString());
            }
        }
}

You just have to use println instead of print:

System.out.println((listA.get(i)).toString());

Alternatively, you can add the line break character \\n

Your code seems to be working so far. If you just want to see what elements are in listA , just print it out:

System.out.println(listA);

Output:

[this is a test, , test, , test test, ]

Note that the extra lines in your input file are also being stored in listA . I'm not sure if that's the behavior you want.

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