0

I created an arraylist in Java called rows. Now I have a text file which contains information regarding a program (for example: DRAMA-Film A-2.5hours-hi) which is put into a string called strLine. strLine is then split and put into the arraylist rows.

Can anyone tell me why nothing is being output with the system.out.println statement?

import java.io.*;
import java.util.*;


class Processing
{
 public static void main(String args[])
  {
  try{

  FileInputStream fstream = new FileInputStream("file.txt");

  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine = null;

  List<String[]> rows = new ArrayList<String[]>();

    while ((strLine = br.readLine()) != null)   {


        String[] row = strLine.split("-");
        rows.add(row);

  }
  System.out.println(rows.toString());

  in.close();
    }catch (Exception e){
  System.err.println("Error: " + e.getMessage());
  }
  }
}
8
  • Do you really mean that nothing is being printed? Commented Mar 14, 2012 at 18:51
  • 1
    Have you debugged it? is it reaching the syso statement? Have you test if you access the file correctly? Commented Mar 14, 2012 at 18:52
  • @Ted Hopp Netbeans gives me no output, despite telling me build successful Commented Mar 14, 2012 at 18:53
  • Sorry I missed something: [[Ljava.lang.String;@1b0bf9a, [Ljava.lang.String;@f32dde] Commented Mar 14, 2012 at 18:53
  • There must be at least something printed. What is printed? Commented Mar 14, 2012 at 18:55

5 Answers 5

3

Your code is printing out the default representation of an array list. The symbols you are seeing output are the String array references that you are storing in this list. If you want more detailed output then you will need to code for it.

for(final String[] row: rows) {
    System.out.println(Arrays.toString(row);
}

As an example.

4
  • All I want is to split the text in the text file and put them into an array which is then put into the arraylist. I did not understand what you actually mean, but aren't strings put as strings into an array? I mean, why did they change ? Commented Mar 14, 2012 at 18:56
  • 1
    You've accomplished your stated goals. The symbols you are seeing printed out e.g. [Ljava.lang.String;@1b0bf9a are array references. The same arrays you are storing into your ArrayList. If you want to see the element in each individual array then you need to use code like I've shown in my answer. Commented Mar 14, 2012 at 18:59
  • I still get the same output :/ Commented Mar 14, 2012 at 19:01
  • @Brian All objects have a toString() method, but it is not magic: some objects don't have a toString() method which provides a string representation of their content in the way you would want to see it. rows is a List<String[]>, and there it doesn't have a toString() method which prints it the way you want (how would it know how you want to separate the String arrays and the String array elements? Commas? Separate lines?). That's why you have to provide your own implementation such as in this answer. Commented Mar 14, 2012 at 19:07
1

You can't just print an array, you need to print the parts of it.

for (String[] row : rows)
{
  for (String part : row)
    System.out.print(part + " ");
  System.out.println();
}
0
1

The default toString() method generates an internal string that represents the unique object. If you want to print a formatted string of the array contents, you can do this:

for (String[] row : rows) {
    System.out.println(Arrays.toString(row));
}
1
  • I still get the same output :/ Commented Mar 14, 2012 at 19:01
0

You're trying to print out an array of String and are expecting it to look like a string.

  System.out.println(rows.toString());

When you call that, by default it prints out every element in the list, but each element, as you've programmed it is an instance of String[], not String. You need to go a step deeper, inception style to get the results you expect.

Try adding a method like so:

    public static void print(List<String[]> list) {

     for(String[] arr : list) {
         StringBuffer buff = new StringBuffer();
         for(int i = 0; i < arr.length; i++) {
             buff.append(arr[i]);
         }
         System.out.println(buff.toString());
     } 
 }

and then call print(rows);

0

The ArrayList implementation of toString() prints the list using the toString() implementation of its elements. Array objects' implementation of toString() doesn't print the array contents; instead, you need to use Arrays.toString(). So you can either iterate over the list and print each array separately (see Ted Hopp's and Perception's answers), or if you really want to print it all in one go you can implement your own print method, something like this:

<T> String collectionOfArrsToString(Collection<T[]> collection) {
    // Adapted from AbstractCollection.toString() implementation
    Iterator<T[]> i = collection.iterator();
    if (!i.hasNext())
        return "[]";

    StringBuilder sb = new StringBuilder();
    sb.append('[');
    for (;;) {
        T[] tArr = i.next();
        sb.append(Arrays.toString(tArr));
        if (!i.hasNext())
            return sb.append(']').toString();
        sb.append(", ");
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.