user4276463
user4276463

Reputation:

Array not printing out correctly

I've tried moving around my curly braces and just the entire structure of this program a bunch and can't seem to point out how to make this print out correctly. I have a text file that looks like this:

Game of Thrones|Action|HBO|50|Favorite
House of Cards|Drama|Netflix|50|Favorite
Huckabee|Bad Show|Fox News|25|Not favorite
Survivor|Reality|NBC|45|Not favorite
The Daily Show with Jon Stewart|Comedy|Comedy Central|30|Favorite
Louie|Comedy|FX|30|Favorite
Sports Center|Sports News|ESPN|60|Favorite
The Big Bang Theory|Comedy|CBS|30|Not favorite
Sesame Street|Educational|PBS|30|Favorite
Chopped|Food Show|Food Network|60|Favorite

I want my console to show this (minus the pipes) with a toString() that I have, which works perfectly fine, but it prints out with 10 copies of each show and I'm not sure what I can go about doing differently to fix this.

Question: How can I make it so the console prints out exactly 1 copy of each show instead of 10?

Driver Code:

   public class TVShowDriver {
public static void main(String[] args) throws FileNotFoundException {

    TVShow[] tvShow = new TVShow[10];
    String tvName = "";
    String genre = "";
    String network = "";
    int runningTime = 0;
    String favorite = "";

    // reads in Shows.txt
    File tvShows = new File("./src/Shows.txt");
    Scanner fileScanner = new Scanner(tvShows);

    // while there is a new line in the data, goes to the next one
    while (fileScanner.hasNextLine()) {
        String line = fileScanner.nextLine();
        Scanner lineScanner = new Scanner(line);
        lineScanner.useDelimiter("\\|");

        // while there is a new attribute to read in on a given line, reads
        // data
        while (lineScanner.hasNext()) {
            tvName = lineScanner.next();
            genre = lineScanner.next();
            network = lineScanner.next();
            runningTime = lineScanner.nextInt();
            favorite = lineScanner.next();

            // creates a show
            for (int i = 0; i < tvShow.length; i++) {
                tvShow[i] = new TVShow(tvName, genre, network, runningTime,
                        favorite);
            }

        }

        // prints out shows
        for (int i = 0; i < 10; i++) {
            System.out.println(tvShow[i]);
        }

    }

}

}

TVShow Class:

public class TVShow {

    private String tvName;
    private String genre;
    private String network;
    private int runningTime;
    private String favorite;

    public TVShow(String tvName, String genre, String network, int runningTime, String favorite)
    {
        this.tvName = tvName;
        this.genre = genre;
        this.network = network;
        this.runningTime = runningTime;
        this.favorite = favorite;
    }

    public String getTvName() {
        return tvName;
    }

    public void setTvName(String tvName) {
        this.tvName = tvName;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getNetwork() {
        return network;
    }

    public void setNetwork(String network) {
        this.network = network;
    }

    public int getRunningTime() {
        return runningTime;
    }

    public void setRunningTime(int runningTime) {
        this.runningTime = runningTime;
    }

    public String getFavorite() {
        return favorite;
    }

    public void setFavorite(String favorite) {
        this.favorite = favorite;
    }

    public String toString()
    {
        return "TV Show Name: " + tvName + ", Genre: " + genre + ", Network: " + network + ", Running Time: " + runningTime + " mins" + ", Favorite: " + favorite;
    }
}

Upvotes: 0

Views: 64

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

This...

    // creates a show
    for (int i = 0; i < tvShow.length; i++) {
        tvShow[i] = new TVShow(tvName, genre, network, runningTime,
                              favorite);
    }

...is wrong. Basically, each time you read a line from the file, you are re-filling the array with that show's details (sure you're making a new instance of TVShow, but it contains all the same details.

Instead, use a separate iteration value and increment each time you read a new line...

int currentLine = 0;
while (lineScanner.hasNext()) {
    if (currentLine < tvShow.length) {
        tvName = lineScanner.next();
        genre = lineScanner.next();
        network = lineScanner.next();
        runningTime = lineScanner.nextInt();
        favorite = lineScanner.next();

        tvShow[currentLine] = new TVShow(tvName, genre, network, runningTime,
                               favorite);
        currentLine++;
    } else {
        System.err.println("The array is full");
        break;
    }

}

Upvotes: 1

dave
dave

Reputation: 12806

I think your problem lies in this piece:

// creates a show
for (int i = 0; i < tvShow.length; i++) {
    tvShow[i] = new TVShow(tvName, genre, network, runningTime,favorite);
}

You seem to be filling up the tvShow array each time with ten (which is the length of the array) copies of the same show.

A solution is to have a counter outside of your first while loop which you increment. Then use that counter to index into tvShow.

Alternatively, if you just want to print each show you could not bother to save them all in an array, create a TVShow variable outside of the while loops and reassign it.

So that would look like: TVShow myShow; // outside of the first while loop loop

myShow = new TVShow(tvName, genre, network, runningTime,favorite); // where you were assigning into the array

Upvotes: 0

Related Questions