Sal
Sal

Reputation: 109

Blank after loading from text file

I am trying to output information about TV shows from a text file which looks like this:

2
PPL
Tuesday
1900
BBT
Thursday
2100

My method to read and output the file looks like this:

//method to read shows from file
public static void loadFile() throws FileNotFoundException, IOException{
    int i;
    int x = 0;
    BufferedReader input = new BufferedReader(new FileReader("TV.txt"));
    x = Integer.valueOf(input.readLine()).intValue();
    System.out.println(x + " shows!");

    for(i = 0; i < show.size(); i++){
        ((showInfo)show.get(i)).name = input.readLine();
        ((showInfo)show.get(i)).day = input.readLine();
        ((showInfo)show.get(i)).time = Integer.valueOf(input.readLine()).intValue();
    }

    System.out.println("Show Information");
    for(i = 0; i < show.size(); i++){
        System.out.println("Name: " + ((showInfo)show.get(i)).name);
        System.out.println("Day: " + ((showInfo)show.get(i)).day);
        System.out.println("Time: " + ((showInfo)show.get(i)).time);
    }    
}  

It shows me the number of shows and "Show Information" but then it's blank and returns to the main menu. Why is it doing this? Oh and please don't ask why I'm using casting and not generics. I can't because I have to use 1.4. My teacher wants it that way.

Any help would be great! Thanks in advance. :)

Upvotes: 1

Views: 49

Answers (1)

Dennis Meng
Dennis Meng

Reputation: 5187

I assume that show is some Collection type.

My best guess is that before you call this function, show doesn't actually have anything yet (i.e. show.size() is 0).

Since x is the number of shows, you should probably be looping like for (int i = 0; i < x; i++), and creating new instances of showInfo using your data, and inserting those into show within your loop.

Upvotes: 2

Related Questions