不知道为什么会收到java.util.NoSuchElementException错误

时间:2018-09-09 22:51:08

标签: java arrays oop if-statement arraylist

我目前正在开发一个程序,该程序会从文本文档中获取信息,然后将其打印到另一个文件中,并从Java中打印出来,但是到了一半,我没有出现此类异常错误,而且我不确定为什么。 测试人员代码:

     import java.util.ArrayList;
    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    public class HurricaneSelectorTester
    {
        public static void main (String [ ] args) throws IOException
        {
            //File scanner
            File fileName = new File("HurricaneData.txt");
            Scanner inFile = new Scanner(fileName);
            PrintWriter outputFile = new PrintWriter(new File("OutputData.txt"));
            Scanner in;
            in = new Scanner(System.in);
            //construct a Scanner object with one line

            //Declare variables
            int arrayLength = 156; 
            int [] years = new int[156];
            int index = 0;
            int cat1 = 0;
            int cat2 = 0;
            int cat3 = 0;
            int cat4 = 0;
            int cat5 = 0;
            double windAvg = 0;
            double windTotal = 0;
            double windMax = Integer.MIN_VALUE;
            double windMin = Integer.MAX_VALUE;
            double pressureAvg = 0;
            double pressureTotal = 0;
            int pressureMax = Integer.MIN_VALUE;
            int pressureMin = Integer.MAX_VALUE;
            double catAvg = 0;
            double catTotal = 0;
            int catMax = Integer.MIN_VALUE;
            int catMin = Integer.MAX_VALUE;
            int rangeMax = 0;
            int rangeMin = 0;

            //Ask for range
            System.out.println("Please enter minimum year (1995-2015)");
            rangeMin = in.nextInt();
            System.out.println("Please enter maximum year (1995-2015)");
            rangeMax = in.nextInt();

            //Print title info
            System.out.println("Hurricanes "+ ""+ rangeMin + "-" + "" + rangeMax);
            System.out.println();
            System.out.println("Year   Hurricane   Category    Pressure (mb)    Wind Speed (mph)");
            System.out.println("****************************************************************");

            ArrayList<HurricaneSelector> hurricanes = new ArrayList<HurricaneSelector>();
            //Load all the info into the arrays
            while (inFile.hasNext())
            {
                hurricanes.add(new HurricaneSelector(inFile.nextInt(),inFile.next(),inFile.nextInt(),inFile.nextInt(),inFile.next()));
            }
            for(index = 0; index < 156; index++){
                years[index] = inFile.nextInt();
            }   
            inFile.close();

            HurricaneSelector dataRecord; //Data record for HurricaneSelector

            for(index = 0; index < 156; index++)
           {if(years[index]>= rangeMin && years[index]<= rangeMax){
               dataRecord = hurricanes.get(index);
               dataRecord.calcCategoriesAndTotals();
               dataRecord.calcNewWind();
               dataRecord.calcWindMax();
               dataRecord.calcWindMin();
               dataRecord.calcPressureMax();
               dataRecord.calcPressureMin();
               dataRecord.calcCategoryMax();
               dataRecord.calcCategoryMin();
           }
           }
           dataRecord = hurricanes.get(index);
           dataRecord.calcWindAverage();
           dataRecord.calcCategoryAverage();
           dataRecord.calcPressureAverage();
            //Print out data
            outputFile.println("Year  Name  Category  Pressure  Wind Speed"); 
            for (index = 0; index < 156; index++){
            if(years[index]>= rangeMin && years[index]<= rangeMax){
              System.out.println(" "+hurricanes.get(index));
              System.out.println();
              outputFile.println(" "+hurricanes.get(index));
            }
            }

            outputFile.close(); 

            System.out.println("****************************************************************");
            System.out.print("      Average:");
            System.out.printf("%15.1f%13.1f%20.2f",catAvg,pressureAvg,windAvg);
            System.out.println();
            System.out.print("      Maximum:");
            System.out.printf("%15d%13d%20.2f",catMax , pressureMax , windMax);
            System.out.println();
            System.out.print("      Minimum:");
            System.out.printf("%15d%13d%20.2f",catMin , pressureMin , windMin);
            System.out.println();
            System.out.println();
            System.out.println("Summary of categories");
            System.out.println("   Category 1: " + cat1);
            System.out.println("   Category 2: " + cat2);
            System.out.println("   Category 3: " + cat3);
            System.out.println("   Category 4: " + cat4);
            System.out.println("   Category 5: " + cat5);

        }

方法:

    public class HurricaneSelector
    {
        private int myYear, myPressure, myWind, myRangeMin, myRangeMax, myCategory, category1, category2,category3, category4, category5;
        private String myMonth, myName;
        private double myNewWind;
        public double myWindAverage, myPressureAverage, myCategoryAverage, myWindMax = Integer.MIN_VALUE,
        myWindMin = Integer.MAX_VALUE, myPressureMax = Integer.MIN_VALUE, myPressureMin = Integer.MAX_VALUE,
        myCatMax = Integer.MIN_VALUE,myCatMin = Integer.MAX_VALUE;
        public int total;

        /**
        * Constructor for objects of type HurricaneSelector
        * @param year is the year of the hurricane
        * @param month is the month of the hurricane
        * @param pressure is the pressure of the hurricane
        * @param wind is the wind speed of the hurricane
        * @param name is the name of the hurricane
        */
        HurricaneSelector(int year, String month, int pressure, int wind, String name)
        {
            myYear = year;
            myMonth = month;
            myPressure = pressure;
            myWind = wind;
            myName = name;
        }

        /**
        * Mutator method to calculate the wind speed in mph (no parameters)
        */
        public void calcNewWind()
        {
            myNewWind =  (myWind* 1.15);
        }

        /**
        * Mutator method to calculate if the value is the new Maximum (no parameters)
        */
        public void calcWindMax()
        {
            if (myNewWind > myWindMax){

                    myWindMax = myNewWind;

                }
        }


        /**
        * Mutator method to calculate if the value is the new Minimum (no parameters)
        */
        public void calcWindMin()
        {
            if (myNewWind > myWindMin){

                    myWindMin = myNewWind;

                }
        }

        /**
        * Mutator method to calculate if the value is the new Maximum (no parameters)
        */
        public void calcPressureMax()
        {
            if (myPressure > myPressureMax){

                    myPressureMax = myPressure;

                }
        }

        /**
        * Mutator method to calculate if the value is the new Minimum (no parameters)
        */
        public void calcPressureMin()
        {
            if (myPressure > myPressureMin){

                    myPressureMin = myPressure;

                }
        }

        /**
        * Mutator method to calculate if the value is the new Maximum (no parameters)
        */
        public void calcCategoryMax()
        {
            if (myCategory > myCatMax){

                    myCatMax = myCategory;

                }
        }

        /**
        * Mutator method to calculate if the value is the new Minimum (no parameters)
        */
        public void calcCategoryMin()
        {
            if (myCategory > myCatMin){

                    myCatMin = myCategory;

                }
        }

        /**
        * Mutator method to calculate which category the Hurricane fits into and get the totals(no parameters)
        */
        public void calcCategoriesAndTotals()
        {
            myWindAverage += myNewWind;
                myPressureAverage += myPressure;
                if (myNewWind > 74 && myNewWind < 95)
                {

                    myCategory = 1;
                    myCategoryAverage += myCategory;
                    category1++;
                }
                else if(myNewWind > 96 && myNewWind < 110)
                {

                    myCategory = 2;
                    myCategoryAverage += myCategory;
                    category2++;
                }   
                else if(myNewWind > 111 && myNewWind < 129)
                {

                    myCategory = 3;
                    myCategoryAverage += myCategory;
                    category3++;
                }
                else if(myNewWind > 130 && myNewWind < 156)
                {

                    myCategory = 4;
                    myCategoryAverage += myCategory;
                    category4++;
                }
                else if(myNewWind > 157)
                {

                    myCategory = 5;
                    myCategoryAverage += myCategory;
                    category5++;

                }
            total++;

        }

        /**
        * Mutator method to calculate the wind speed average (no parameters)
        */
        public void calcWindAverage()
        {
            myWindAverage = myWindAverage/total;
        }

        /**
        * Mutator method to calculate the category average (no parameters)
        */
        public void calcCategoryAverage()
        {
            myCategoryAverage = myCategoryAverage/total;
        }

        /**
        * Mutator method to calculate the pressure average (no parameters)
        */
        public void calcPressureAverage()
        {
            myPressureAverage = myPressureAverage/total;
        }

        /**
        * Getter method to return the year of the hurricane (no parameters)
        */
        public int getYear()
        {
           return myYear;
        }

        /**
        * Getter method to return the month of the hurricane (no parameters)
        */
        public String getMonth()
        {
           return myMonth;
        }

        /**
        * Getter method to return the pressure of the hurricane (no parameters)
        */
        public int getPressure()
        {
           return myPressure;
        }

        /**
        * Getter method to return the wind speed of the hurricane (no parameters)
        */
        public double getNewWind()
        {
           return myNewWind;
        }

        /**
        * Getter method to return the name of the hurricane (no parameters)
        */
        public String getName()
        {
           return myName;
        }

        /**
        * Getter method to return the wind average (no parameters)
        */
        public Double getWindAverage()
        {
           return myWindAverage;
        }

        /**
        * Getter method to return the pressure average (no parameters)
        */
        public Double getPressureAverage()
        {
           return myPressureAverage;
        }

        /**
        * Getter method to return the category average (no parameters)
        */
        public Double getCategoryAverage()
        {
           return myCategoryAverage;
        }

        /**
        * Getter method to return the category maximum (no parameters)
        */
        public Double getWindMax()
        {
           return myWindMax;
        }

        /**
        * Getter method to return the category minimum (no parameters)
        */
        public Double getWindMin()
        {
           return myWindMin;
        }

        /**
        * Getter method to return the category maximum (no parameters)
        */
        public Double getPressureMax()
        {
           return myPressureMax;
        }

        /**
        * Getter method to return the category minimum (no parameters)
        */
        public Double getPressureMin()
        {
           return myPressureMin;
        }

        /**
        * Getter method to return the category maximum (no parameters)
        */
        public Double getCategoryMax()
        {
           return myCatMax;
        }

        /**
        * Getter method to return the category minimum (no parameters)
        */
        public Double getCategoryMin()
        {
           return myCatMax;
        }

        public String toString(){
            return String.format("%10d%10s%10d%10d%10.2f",myYear,myName, myCategory, myPressure, myNewWind); 
        }

文本文件的链接:https://drive.google.com/file/d/1eazHCEwT0Se6hfx2SDilqCqY8ZMi4E9k/view?usp=sharing https://drive.google.com/file/d/1CN0JnlbMWNEB7B4nomgJ_-6mkwR1wgYc/view?usp=sharing

我知道代码还有其他问题,例如无用的变量,格式等。但是现在我需要修复程序,以便它可以实际运行并立即打印出大部分数据。

1 个答案:

答案 0 :(得分:0)

您需要reset()扫描程序,因为您已到达循环的结尾,并且您希望从头开始进行下一组操作。

来自JavaSE docs

  

public int nextInt()

     

将输入的下一个标记扫描为整数。

     

对该方法的调用nextInt()的行为与调用nextInt(radix)的行为完全相同,其中radix是此扫描器的默认基数。

     

返回:       从输入扫描的整数

     

投掷:

     

InputMismatchException-如果下一个标记与Integer正则表达式不匹配或超出范围

     

NoSuchElementException-如果输入已用尽

     

IllegalStateException-如果此扫描仪已关闭