Printing Java ArrayList contents line by line in console

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) { //Add to ArrayList demo.add(line); System.out.println(demo); } reader.close(); }catch (Exception e) { System.out.println("Error: "+e.getMessage()); System.exit(0); } } } 

The result obtained:

 cat cat, rat cat, rat, hat 

Expected Result:

 cat rat hat 
+4
source share
9 answers

The result you see every time is the result of ArrayList.toString () being printed to standard. Instead, scroll through the ArrayList after you finish reading the contents of the file in the ArrayList:

  while ((line=reader.readLine()) !=null) { //Add to ArrayList demo.add(line); } reader.close(); //do fun stuff with the demo ArrayList here for (String s : demo) { System.out.println(s); } 
+5
source

Line:

 System.out.println(demo); 

Must be:

 System.out.println(line); 

This, however, will perform both reading and printing in a single cycle. You may need to do the following after the first loop of building the array:

 for (String line : demo) { System.out.println(line); } 
+5
source

You need to print "line", not "demo". In fact, printing a demo calls the ToString () method in an ArrayList, which probably prints a sequence of array elements.

So:

 demo.add(line); System.out.println(line); 
+4
source
 //Add to ArrayList demo.add(line); System.out.println(demo); 

You add a line to the list, and then print the entire list. Everytime.

+3
source

You are just printing an ArrayList, not its members. You need to iterate through ArrayList and print them as you wish (with a comma and a space between the elements).

+2
source

System.out.println(demo); must be out of cycle.

And the demo will be printed with a comma separating the lines.

You will need to create another loop to repeat the iteration. And print each element with println.

+1
source

Yes, you print the entire list every time, because you both read and write in the same loop.

You will get the expected result if you do all the reading in one loop and then another for writing.

Here's how I write it:

 public class ArrayListDemo { public static void main (String [] args) { try { List<String> demo= readList(new FileReader("hi.tpl"), "\\s+"); writeList(demo, System.out); } catch (Exception e) { e.printStackTrace(); } } private static List<String> readList(Reader reader, String splitRegex) throws IOException { List<String> values = new ArrayList<String>(); BufferedReader br = null; try { br = new BufferedReader(reader); String line; while ((line = reader.readLine()) != null) { String [] tokens = line.split(splitRegex); for (String token : tokens) { values.add(token); } } } finally { try { if (br != null) br.close(); } catch (IOException e) { e.printStackTrace(); } } return values; } private static void writeList(List<String> list, PrintStream p) throws IOException { if (list != null && p != null) { for (String s : list) { p.println(s); } } } } 

One more tip: style matters. You are careless with placement and spacing. Choose a style and be consistent in that. Your code is harder to read and understand if you do not.

+1
source

You display the ArrayList instruction object through System.out.println(demo); You really need to display the contents of the ArrayList line by line, using a loop, using the statement System.out.println(line);

It is better to use foreach , namely:

 for (String line : demo) { System.out.println(line); } 
+1
source

Try it if you are using Java 8 :

 demo.forEach(System.out::println); 
0
source

Source: https://habr.com/ru/post/1389820/


All Articles