数组错误java.util.NoSuchElementException:

时间:2018-12-06 17:39:23

标签: arrays string file int java.util.scanner

我需要从文本礼物中读取文件并将其放入数组中。同样,我需要阅读文本文件的费用。

程序需要打印当天的数据,并获取当天的费用和总费用。

我很困惑为什么会发生错误。感谢您的帮助! :D

我的代码:

    import java.io.File;

    import java.io.FileNotFoundException;

    import java.util.Scanner;

    import java.text.NumberFormat;

    public class GiftsTwelveDays{

        public static void main(String[] args) {

            String[] gifts = new String[12];

            double[] costGifts = new double[12];

            String total = new String();

            //Following code reads the Gifts.txt file

            File file = new File("gifts.txt");

            try 
            {
                Scanner scannerFile = new Scanner(file);

                while (scannerFile.hasNextLine()) 

                {
                    int i = scannerFile.nextInt();

                    String present = scannerFile.nextLine();

                    gifts[i - 1] = present;

                }

                scannerFile.close();

            }

            catch (FileNotFoundException e) 

            {
                System.out.println("File not Found.");
            }



            //This reads the cost and stores in array of double

            File file1 = new File("cost.txt");

            try 
            {
                Scanner scannerFile = new Scanner(file1);

                while (scannerFile.hasNextLine()) {

                    scannerFile.next();

                    if(scannerFile.hasNextInt())

                    {

                        int i = scannerFile.nextInt();

                        double cost = scannerFile.nextDouble();

                        costGifts[i-1] = cost;

                    }

                    else
                    {

                        scannerFile.next();

                        total = scannerFile.nextLine();

                    }

                }

                scannerFile.close();

            }

            catch (FileNotFoundException e) 
            {

                System.out.println("File could not be found");;

            }



            //following gets user input and runs

            Scanner userInput = new Scanner(System.in);

            NumberFormat money = NumberFormat.getInstance(); //for format with comma

            money.setGroupingUsed(true);

            double dayCost = 0;

            System.out.println("Enter the day:");

            int choice = userInput.nextInt();

            userInput.nextLine();

            if (choice < 1 || choice > 12)//runs if choic is invalid

            {

                System.out.println("Invalid Choice");

            }

            else

            {

                System.out.println("Your gifts for the day " + choice + " are: ");

                //calculates the day cost and prints gift simultaneously

                for(int i = 0; i < choice; i++)

                {

                    System.out.println((i+1) + gifts[i]);

                    dayCost = dayCost + costGifts[i];

                }

                //prints the calculated cost.

                System.out.println("Cost of Day:$" + money.format(dayCost));

                System.out.println("Total Cost for Twelve Days: :$" + total);

            }

        }

    }

消息错误:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at GiftsTwelveDays.main(GiftsTwelveDays.java:77)

0 个答案:

没有答案