[英]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. 我希望我的控制台使用我拥有的toString()来显示此内容(减去管道),这可以很好地工作,但是它会打印出每个节目10份,我不确定该如何做些不同的事情解决这个问题。
Question: How can I make it so the console prints out exactly 1 copy of each show instead of 10? 问题:我该如何做才能使控制台准确地打印出每个节目的1个副本,而不是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;
}
}
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. 基本上,每次您从文件中读取一行时,都会用该节目的详细信息重新填充数组(确保您正在制作
TVShow
的新实例,但它包含所有相同的详细信息。
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;
}
}
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. 您似乎每次都用同一节目的十个副本(即数组的长度)填充
tvShow
数组。
A solution is to have a counter outside of your first while loop which you increment. 一种解决方案是在您的第一个while循环之外增加一个计数器。 Then use that counter to index into
tvShow
. 然后使用该计数器索引到
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. 另外,如果您只想打印每个节目,则不必费心将它们全部保存在一个数组中,可以在while循环之外创建一个TVShow变量,然后重新分配它。
So that would look like: TVShow myShow; // outside of the first while loop loop
这样看起来像:
TVShow myShow; // outside of the first while loop loop
TVShow myShow; // outside of the first while loop loop
myShow = new TVShow(tvName, genre, network, runningTime,favorite); // where you were assigning into the array
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.