Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would take the following approach:</p> <pre><code>import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class ReadTextFile { public static void main(String[] args) throws FileNotFoundException { File file = new File("test.txt"); Scanner scanner = new Scanner(file); List&lt;Result&gt; results = new ArrayList&lt;Result&gt;(); while(scanner.hasNextLine()) { String currentLine = scanner.nextLine(); String [] resultArray = currentLine.split(" "); results.add(new Result(resultArray[0], resultArray[1], resultArray[2])); } scanner.close(); } private static class Result { private String date; private String time; private String kg; public Result(String date, String time, String kg) { super(); this.date = date; this.time = time; this.kg = kg; } public String getDate() { return date; } public String getTime() { return time; } public String getKg() { return kg; } } } </code></pre> <p>Now you can pull out any information that you want to from the list of results that you have.</p> <p>So if you wanted to print everything, you could do the following:</p> <pre><code>for(Result singleResult : results) { System.out.println(singleResult.getDate() + " " + singleResult.getTime() + " " + singleResult.getKg()); } </code></pre> <p>You basically can do whatever you want to with the data. This approach would also allow you to transform the data into different types before you even create the Result object.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload