Printing Java ArrayList contents line by line to console

44,123

Solution 1

The output you are seeing each time is the result of ArrayList.toString() being printed to standard out. Instead, loop through the ArrayList after you've finished reading the contents of the file into 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); }

Solution 2

The line:

System.out.println(demo);

Should be:

System.out.println(line);

This will, however, do both the reading and printing in one loop. You may be required to do the following after the first loop building the array:

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

Solution 3

You have to print "line" not "demo". In fact printing demo call the ToString() method of the ArrayList that probably print the sequence of the array elements.

So:

demo.add(line);
System.out.println(line);

Solution 4

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

You add the line to the list, then print the whole list. Each time.

Solution 5

You're just printing the ArrayList, not its members. You need to iterate through the ArrayList, and print them as you wish (with the comma and space between items).

Share:
44,123
Luinithil
Author by

Luinithil

Updated on January 29, 2020

Comments

  • Luinithil
    Luinithil over 4 years

    I'm very new to Java programming, and this is actually part of a problem I need to solve for homework: I am reading the contents of a file line by line as a String into an ArrayList for later processing. I need the program to print out to console the contents of the ArrayList on separate lines, but the output after I run 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 is supposed to work is that the program will take my file, the FileReader and BufferedReader will grab the lines of text in the file as Strings, which are then placed in the ArrayList with each String at a different position in the ArrayList right? Can someone please tell me where in the while loop I'm going 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);
            }
        }
    }
    

    Obtained output:

    cat
    cat, rat
    cat, rat, hat
    

    Expected output:

    cat
    rat
    hat
    
  • Dan Hardiker
    Dan Hardiker over 12 years
    This would cause cat, rat, hat, not the desired output of being on separately lines.
  • Perception
    Perception over 12 years
    This is showing alot of code for someone who is doing homework.
  • Luinithil
    Luinithil over 12 years
    Thanks brainzzy! I might need that second loop, not too sure yet.
  • duffymo
    duffymo over 12 years
    How else should someone learn? Why is homework allowed to be a mess?
  • Perception
    Perception over 12 years
    Well, it doesn't have to be a mess but I doubt they will learn if all the code is handed to them on a silver platter.
  • duffymo
    duffymo over 12 years
    First of all, let's remember that the OP posted their entire class. Nothing was done for them. Second, if you look at what I posted, you'll see that all I did was take the original code and refactor it into two methods. It's a lesson in "don't put everything in a main method." Nothing wrong with that. You posted nothing. Do you waste a lot of your time here posting comments on the efforts of others?
  • Luinithil
    Luinithil over 12 years
    @duffymo I need to be able to tokenize the Strings as well as search the ArrayList: will using the List implementation be better for that rather than simply creating the ArrayList directly?
  • duffymo
    duffymo over 12 years
    List or ArrayList has nothing to do with tokenizing. My preference is to prefer the interface type instead of the concrete unless I have a very good reason for doing so. Are you saying that you need to tokenize the line and then print out the List? I'll change the code to do it.
  • Luinithil
    Luinithil over 12 years
    @duffymo I don't really need to print out the List-- the lines of the files I'm reading into the ArrayList are a set of program instructions that need to be tokenized and searched-- I'm writing a very basic interpreter for a very basic language that has less than 10 reserved words. Not really sure how to get it all built really.