简体   繁体   中英

Storing Strings from a Text File into an array including different lines

at the moment I am trying to read in a text file of strings and put them into an array for each line. The text file has 10 lines and each line has a string of words (sentences extracted from a website). My aim is store these strings into arrays (an array for each line).

This is the code I am using at the moment package array;

public class sep_arr
{
    public static void main (String[] args) throws FileNotFoundException
    {

        Scanner DataFile = new Scanner(new File("EntityTagged.txt"));

        while(DataFile.hasNextLine()){
            String line = DataFile.nextLine();

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

            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(",");
            while(scanner.hasNextDouble()){
                salesData.add(scanner.toString());
            }
            scanner.close();

            System.out.println(salesData);
        }

        DataFile.close();

    }
}

The code reads in the 'EntityTagged.txt' file which has the 10 different sentences in and looks to store them in an array. At the moment, when executed, it returns empty arrays

[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]

My goal is to store the sentences in these arrays to then use the 'endsWith() method' to extract specific words from the text.

Any help will be much appreciated

Thank you

There are better ways to read a file than using a scanner with the addition of java.nio in 1.7

As far as parsing each line is concerned, if you're sure that you've got doubles separated by commas and that's what you're trying to create then go for it.

This idiom should work for you:

   Path file = Paths.get("EntityTagged.txt");
    List<String> fileContents = new LinkedList<>();
    try (BufferedReader reader = Files.newBufferedReader(file, Charset.defaultCharset()))
    {
        String line;
        while ((line = reader.readLine()) != null)
        {
            fileContents.add(line);
        }
    }
    return fileContents;

If you want to post a sample of the data, I'll help you figure out if you're tokenizing each line correctly also.

I think your problem is in here

while(scanner.hasNextDouble()){
    salesData.add(scanner.nextDouble()+""); // you wrote scanner.toString()
}

Do in this way

Scanner DataFile = new Scanner(new File("EntityTagged.txt"));

ArrayList<String> salesData = new ArrayList<String>();
while (DataFile.hasNextLine()) {
    String line = DataFile.nextLine();

    salesData.add(line);
}

for (String str : salesData) {
    System.out.println(str);
}

DataFile.close();

If you work with java8 you can do it like this:

Files.lines(Paths.get("EntityTagged.txt")).toArray(String[]::new);

java.nio.file.Files is a utility class for working with files, it has a new method in java8 lines , this method return a stream of strings, this stream is then transformed into an array of strings using the method toArray with the method reference to the "constructor" of String[]

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