我正在尝试从我的Movie class
中读取一个对象数组中的文件,然后打印结果。
public class Movie_Class {
private int MovieID;
private String MovieTitle;
private String Director;
private String Writer;
private String Duration;
private String Genre;
private String Classification;
private String ReleaseDate;
private Double Rating;
public Movie_18512117(int ID, String Title, String Mdirector, String Mwriter,
String Mduration, String Mgenre, String Mclassification, String MreleaseDate, Double Mrating) {
MovieID = ID;
MovieTitle= Title;
Director= Mdirector;
Writer= Mwriter;
Duration= Mduration;
Genre= Mgenre;
Classification= Mclassification;
ReleaseDate= MreleaseDate;
Rating= Mrating;
}
public int getMovieID(){
return MovieID;
}
public String getMovieTitle(){
return MovieTitle;
}
public String getDirector(){
return Director;
}
public String getWriter(){
return Writer;
}
public String getDuration(){
return Duration;
}
public String getGenre(){
return Genre;
}
public String getClassification(){
return Classification;
}
public String getReleaseDate(){
return ReleaseDate;
}
public Double getRating(){
return Rating;
}
public void setMovieID(int MovieID) {
this.MovieID = MovieID;
}
public void setMovieTitle(String MovieTitle){
this.MovieTitle = MovieTitle;
}
public void setDirector(String Director) {
this.Director = Director;
}
public void setWriter(String Writer) {
this. Writer = Writer;
}
public void setDuration(String Duration) {
this. Duration = Duration;
}
public void setGenre(String Genre) {
this.Genre = Genre;
}
public void setClassification(String Classification){
this.Classification = Classification;
}
public void setReleaseDate(String ReleaseDate){
this.ReleaseDate = ReleaseDate;
}
public void setRating(Double Rating){
this.Rating = Rating;
}
}
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class movie {
public static void main(String[] args) throws FileNotFoundException {
File myFile = new File ("movieLibrary.txt");
Scanner inputFile = new Scanner(myFile);
String str;
Movie_Class[] movie = new Movie_Class[100];
String[] tokens;
while (inputFile.hasNext()){
str=inputFile.next();
tokens = str.split(",");
for(int i = 0; i < movies.length; i++){
movie[i] = new Movie_18512117(1);
//I don't know how to read the lines in to //my array and split the spaces, then print the result.
System.out.println(movie);
}
}
inputFile.close();
}
}
答案 0 :(得分:0)
如果要像您一样迭代输入流,请尝试使用BufferedReader和FileReader类而不是扫描程序。在链接中是一个有用的教程如何执行此操作:
https://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/
另外,您可以使用&#34; .split(正则表达式)&#34;用于进一步处理来自文件的传入信息的方法。 (例如,在String.split(&#34; \ s +&#34;);)
的空格上拆分至于向阵列添加信息;您可以尝试使用ArrayList及其.add()方法,而不是现在使用的对象数组。
答案 1 :(得分:0)
Movie_Class
的构造函数方法应该是public Movie_Class
,而不是public Movie_18512117
。这是第一个错误。详细了解constructors。
然后,您可以通过调用
Movie_Class
对象
movie[i] = new Movie_Class(...);
在构造函数中,您必须输入电影的所有细节,而不仅仅是ID
。如果要创建仅包含ID的影片对象,则应定义仅使用ID
作为输入值的新构造函数,如下所示
public Movie_Class (int ID) {
// Do whatever you want
}
如果要添加对象,可以使用ArrayList而不是Array,而不知道应该存储在其中的确切元素数。阅读 ArrayLists 和其他集合 here。 首先尝试修复这些错误。然后你可以继续。