【问题标题】:Not sure why i'm getting an java.util.NoSuchElementException error不知道为什么我收到 java.util.NoSuchElementException 错误
【发布时间】:2019-02-14 09:11:41
【问题描述】:

我目前正在开发一个程序,我从文本文档中获取信息,然后将其打印到另一个文件中并从 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

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

【问题讨论】:

  • 我知道它很长,但如果有人能告诉我出了什么问题并帮助我解决它,我将不胜感激,谢谢。
  • 请发布完整的堆栈跟踪以及错误发生在哪一行
  • java.util.Scanner.throwFor(Scanner.java:862) 的 java.util.Scanner.next(Scanner.java:1485) 的 java.util.Scanner 的 java.util.NoSuchElementException。 nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at HurricaneSelectorTester.main(HurricaneSelectorTester.java:68)

标签: java arrays oop if-statement arraylist


【解决方案1】:

您需要reset() Scanner,因为您已经到达循环的末尾,并且您想从头开始进行下一组操作。

来自JavaSE docs:

public int nextInt()

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

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

返回: 从输入扫描的 int

投掷:

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

NoSuchElementException - 如果输入已用尽

IllegalStateException - 如果此扫描仪已关闭

【讨论】:

    猜你喜欢
    • 2017-11-23
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多