如何使用扫描仪将项目添加到阵列列表

时间:2019-04-24 19:46:17

标签: java arraylist java.util.scanner

我要从两个文件中获取电影项目,并使用扫描仪读取文件内容,将这两个内容都添加到Arraylist“ listOne”和“ listTwo”中。然后将这两个列表合并为一个。但是,尽管扫描程序可以正常读取文件,但我的.add似乎没有将它们正确添加到ArrayLists中。

我确保我的合并和印刷机都能正常工作。完成该操作后,我会在扫描仪正在读取的内容以及添加后的列表内容上进行打印。扫描仪会显示正确的信息(即A,J.Edgar,2011,R,1),但是列表内容为我提供了基本值(即[标题:,年份:0,评分:空,查看:0;] )。

//这是我的主班

 Scanner fileScanner; //Scanner to read TestInput1.txt
 Scanner fileScannerTwo; //Scanner to read TestInput2.txt
 ArrayList<Movie>listOne = new ArrayList<Movie>(); //first list
 ArrayList<Movie>listTwo = new ArrayList<Movie>(); //second list
 ArraryList<Movie>outList = new ArrayList<Movie>(); //merged&sorted list

 try{
      fileScanner = GetInputFile("Enter input filename: ");
      while(fileScanner.hasNextLine())
      {
           String[] elements = fileScanner.nextLine().split(",");
           if(elements[0].equals("A")) //each movie has "A" to give ok
           {
                listOne.add(new Movie(elements[1], 
                Integer.parseInt(elements[2]), elements[3], 
                Integer.parseInt(elements[4])));
           }
      }
      //The same code but replace "listOne" with "listTwo"
 ..........
 ..........
      //PrintWriter that works properly
 ..........
 ..........

 }//end of try
 catch(FileNotFoundException ex){
      System.out.println("User terminated program");
 }

''''''''''''''''''''''''''''''''''''''''''''' '

 //this is my Movie class
 private String movieTitle;
 private int releaseYear;
 private String rating;
 private int movieReview;

 public Movie(String Title, int Year, String rating, int Review)
 {
      movieTitle = "";
      releaseYear = 0;
      rating = "";
      movieReview = 0;
 }
 public String getTitle()
 {
      return movieTitle;
 }
 public int getYear()
 {
      return releaseYear;
 }
 public String getRating()
 //same stuff
 public int getReview()
 //same stuff

 public int compareTo(Movie other) //haven't used this yet, but will to sort
 {
    int makeComparison = this.movieTitle.compareTo(other.movieTitle);

    if(makeComparison != 0)
        return makeComparison;

    else
    {
        if(this.releaseYear != other.releaseYear)
            return this.releaseYear - other.releaseYear;

        else
            return 0;
    }
 }

 public String toString()
 {
      return String.format("Title: %s, Year: %s, Rating: %s, Review: %s;", 
                           this.movieTitle, this.releaseYear, this.rating, 
                           this.movieReview);
 }

我希望输出看起来像  [标题:J.Edgar,年份:2011,评分:R,评论:1] 但我明白了  [Title:,Year:0,Rating:null,Review:0;]

以下是文件的示例: A,J。埃德加,2011,R,1

A,《印第安纳·琼斯与厄运神殿》,1984年,PG,1

A,母亲节,2016年,PG-13,4

A,世界历史第一部分,1981,R,1

A,鳄鱼猎人,2002,PG,2

1 个答案:

答案 0 :(得分:0)

发现问题:

public Movie(String Title, int Year, String rating, int Review)
{
    movieTitle = "";
    releaseYear = 0;
    rating = "";
    movieReview = 0;
}

您没有使用任何构造函数参数。您只是总是将标题设置为“”,发行年份设置为0,依此类推。

尝试以下方法:

public Movie(String Title, int Year, String rating, int Review)
{
    movieTitle = Title;
    releaseYear = Year;
    this.rating = rating;
    movieReview = Review;
}

此外,请考虑遵循Java命名和代码标准。普遍接受的标准是始终以小写字母开头变量名,编写构造函数时要使用与类成员完全相同的名称,因此:

public Movie(String movieTitle, int releaseYear, String rating, int movieReview) {
    this.movieTitle = movieTitle;
    this.releaseYear = releaseYear;
    this.rating = rating;
    this.movieReview = movieReview;
}