I am very new to Java programming, and this is actually part of the problem I need to solve for homework: I read the contents of the file line by line as a String in an ArrayList for later processing. I need a program for printing, so that the console contents ArrayList in separate lines, but the output after running the compiled file prints the first line of the file, then prints the first and second lines together on the next line, then prints the first, second and third lines of the program.
My understanding of how this should work is that the program will take my file, FileReader and BufferedReader will capture lines of text in the file as lines that are then placed in an ArrayList with each line at a different position in the ArrayList? Can someone please tell me where in the while loop I am wrong? Thanks!
code:
public class ArrayListDemo { public static void main (String[]args) { try { ArrayList<String> demo= new ArrayList <String>(); FileReader fr= new FileReader("hi.tpl"); BufferedReader reader= new BufferedReader(fr); String line; while ((line=reader.readLine()) !=null) {
The result obtained:
cat cat, rat cat, rat, hat
Expected Result:
cat rat hat
source share