我只想说,在您阅读其余部分之前,我只想弄清楚为什么我的输出打印这些错误或 null 的问题,正如我解释的那样。如果您需要任何其他信息,请告诉我谢谢!
嘿伙计们,所以我正在尝试打印出我的文本文件,当我输入要打印的文件部分时,我打印了最大值和最小值,并将其作为我的输出。这适用于我的查询的每个变量,它打印出相同的东西。
Please input your rating file name
project 2.txt
Please pick inquiry: ratingPercentage, views, ratingScore, movieName,
Release, exit
Release
The highest is null
null
The lowest is null
null
以下是我用来打印文本文件的所有文件。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
public class Movie implements Comparable<Movie>
{
String movieName;
long ratingPercentage;
int StudThatSeenMov;
float averageRating;
int movieReleaseYear;
public Movie()
{
movieName = "";
ratingPercentage = 0;
StudThatSeenMov = 0;
averageRating = 0;
movieReleaseYear = 0;
}
public Movie(long ratingPer,int STSM,float averRate,String movName,int movReleaseYear)
{
movieName = movName;
ratingPercentage = ratingPer;
StudThatSeenMov = STSM;
averageRating = averRate;
movieReleaseYear = movReleaseYear;
}
public String getMovie()
{
return movieName;
}
public long getRate()
{
return ratingPercentage;
}
public int getViews()
{
return StudThatSeenMov;
}
public float getAverage()
{
return averageRating;
}
public int getMovieYear()
{
return movieReleaseYear;
}
public void setMovieName(String movName)
{
movieName = movName;
}
public void setRate(long ratingPer)
{
ratingPercentage = ratingPer;
}
public void setViews(int STSM)
{
StudThatSeenMov = STSM;
}
public void setAverage(float averRate)
{
averageRating = averRate;
}
public void setMovieRelease(int movReleaseYear)
{
movieReleaseYear = movReleaseYear;
}
@Override
public int compareTo(Movie o) {
return (this.movieName.compareTo(o.movieName));
}
public String toString()
{
return "" +averageRating + "t" + StudThatSeenMov + "t" + movieName + "t"+ movieReleaseYear;
}
}
class MovRating implements Rating<Movie>
{
String ratingFile;
ArrayList<Movie> movies = null;
MovRating(String ratingFile)
{
this.ratingFile= ratingFile;
movies = readInputFile();
}
ArrayList<Movie> getMovies()
{
return movies;
}
void setMovie(ArrayList<Movie> movies)
{
this.movies = movies;
}
private ArrayList<Movie> readInputFile()
{
ArrayList<Movie> lmovie = new ArrayList<>();
if(this.ratingFile == null)
return null;
Scanner input;
try{
input = new Scanner(new File(this.ratingFile));
while(input.hasNextLine())
{
String line = input.nextLine();
String[] elements = line.split("t");
if(elements.length != 5) continue;
String tmp = (new StringBuilder(elements[0])).reverse().toString();
long ratingPercentage = Long.parseLong(tmp.trim(),10);
int StudThatSeenMov = Integer.parseInt(elements[1].trim());
float AverageRating = Float.parseFloat(elements[2].trim());
int movieReleaseYear = Integer.parseInt(elements[4].trim());
Movie m = new Movie(ratingPercentage,StudThatSeenMov,AverageRating,elements[3],movieReleaseYear);
lmovie.add(m);
}
input.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
return(lmovie);
}
protected void produceOutput(ArrayList<Movie> movies, String query)
{
Comparator<Movie> cp = null;
switch(query)
{
case"ratingPercentage":
cp = new Comparator<Movie>()
{
public int compare(Movie m1, Movie m2)
{
return (int) (m2.ratingPercentage-m1.ratingPercentage);
}
};
case"ratingScore":
cp = new Comparator<Movie>()
{
public int compare(Movie m1, Movie m2)
{
return (int) (m2.averageRating-m1.averageRating);
}
};
break;
case"Views":
cp = new Comparator<Movie>()
{
public int compare(Movie m1, Movie m2)
{
return (int) (m2.StudThatSeenMov-m1.StudThatSeenMov);
}
};
break;
case"movieName":
cp = null;
break;
case"Release":
cp = new Comparator<Movie>()
{
public int compare(Movie m1,Movie m2)
{
return (int) (m2.movieReleaseYear - m1.movieReleaseYear);
}
};
break;
default:
System.out.println("Wrong output type");
return;
}
System.out.println(pickMax(cp));
System.out.println(pickMin(cp));
}
@Override
public Movie pickMax(Comparator<Movie> m)
{
MyPriorityQueue<Movie> pq = new MyPriorityQueue<>(movies,m);
Movie m1 = pq.poll();
System.out.println("The highest is " + m1);
return m1;
}
@Override
public Movie pickMin(Comparator<Movie> m)
{
if(m == null)
{
System.out.println("Reversing default comparable is not implemented");
return pickMin(m);
}else{
Comparator<Movie> c = new Comparator<Movie>()
{
public int compare(Movie m1,Movie m2)
{
return(c.compare(m2, m1));
}
};
MyPriorityQueue<Movie> pq = new MyPriorityQueue<>(movies,c);
Movie m2 = pq.poll();
System.out.println("The lowest is " + m2);
return m2;
}
}
@Override
public void add(Movie e)
{
movies.add(e);
}
@Override
public boolean isEmpty()
{
if((movies == null) || (movies.size() == 0))
return true;
else return false;
}
@Override
public int size()
{
if((movies == null) || (movies.size() == 0))
return 0;
else return movies.size();
}
}
我将其用作要打印的输入文本文件
Project 2.txt file
0000022101 197 6.5 "Rampage" 2018
0000122101 24,139 6.0 "Pacific Rim Uprising" 2018
0000001221 78,114 7.9 "Ready Player One" 2018
0000122001 523 5.9 "Amateur" 2018
0000001212 223,659 7.8 "Black Panther" 2018
0000013200 138,664 7.0 "Jumanji:Welcome to the Jungle" 2017
0000012101 235,515 6.7 "Justice League" 2017
0000001221 484,477 7.8 "Captain America: Civil War" 2016
0000012101 528,313 6.6 "Batman v Superman:Dawn of Justice" 2016
0000001211 661,462 7.8 "Gravity" 2013
打印输出
import java.util.ArrayList;
import java.util.Scanner;
public class QueryEngine {
public static void main(String[] args) {
System.out.println("Please input your rating file name");
String ratingFile= new Scanner(System.in).nextLine();
MovRating myRating = new MovRating(ratingFile);
ArrayList<Movie> movies = myRating.getMovies();
boolean moreQuery = true;
while(moreQuery)
{
System.out.println("Please pick inquiry: ratingPercentage, " + "views, ratingScore, movieName, Release, exit" );
String query = new Scanner(System.in).nextLine();
if(query.equals("exit")) moreQuery = false;
else myRating.produceOutput(movies, query);
}
}
}
在 main 方法中,您将文件读入一个变量movies
稍后作为参数发送给produceOutput
但一旦在该方法中您不使用该参数,因此当稍后调用pickMin
和pickMax
时,它们使用的是 null 的成员movies
,因为它从未被分配过。
将movies
作为参数添加到pickMin
并pickMax
或使用produceOutput
中的movies
参数调用setMovie
(错误名称 btw(。
还要学习如何使用调试器,它是一个非常有用且功能强大的工具。