打印文本文件的最小值和最大值,并返回null

时间:2018-05-11 19:58:22

标签: java null

我只想在你阅读其余内容之前说一下我只是想弄清楚为什么我的输出会打印这些错误或者我解释为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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在您的main方法中,您正在将文件读入变量movies,稍后您将其作为参数发送到produceOutput,但在该方法中,您不会使用该参数,因此{{1}稍后调用它们pickMin它们使用的成员pickMax是空的,因为它从未被分配过。

movies作为参数添加到moviespickMin,或使用pickMax setMovie参数调用movies(错误名称btw)

还学习如何使用调试器,它是一个非常有用和强大的工具。