java—不使用数组打印输出和从文件读取

r7s23pms  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(347)

程序的功能是从两个独立的文本文件中读取,一个文本文件称为礼品(随机名称)和成本(每天的总成本)。我必须打印如果用户输入是3,它将打印与这3天的费用3行。另外,我还要打印12天的总费用。
我的代码

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.hasNextInt()) {

                scannerFile.next();

                if(scannerFile.hasNextInt())

                {

                    int i = scannerFile.nextInt();

                    double cost = scannerFile.nextDouble();

                    costGifts[i-1] = cost;

                }

                else
                {

                    scannerFile.nextInt();

                    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 costDay = 0;

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

        int choice = userInput.nextInt();

        userInput.nextLine();

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

        {

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

        }

        else

        {

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

            //calculates the day cost and prints gift simultaneously

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

            {

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

                costDay = costDay + costGifts[i];

            }

            //prints the calculated cost.

            System.out.println("\nCost of Day: $" + money.format(costDay));

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

        }

    }

}

输出:

Enter the day:
3
Your gifts for the day 3 are: 

1 Patridge in a Pear Tree
2 Turtle Doves
3 French Hen

Cost of Day: $0

Total Cost for Twelve Days: $

礼品.txt

1 Patridge in a Pear Tree
2 Turtle Doves
3 French Hen
4 Calling Birds
5 Gold Rings
6 Geese-a-Laying
7 Swans-a-Swimming
8 Maids-a-Milking
9 Ladies Dancing
10 Lords-a-Leaping
11 Piper
12 Drummers Drumming

成本.txt

220.13
595.13
776.63
1376.59
2126.59
2516.59
15641.59
15699.59
23252.43
33252.43
36056.83
39094.93
a8jjtwal

a8jjtwal1#

问题是您在评估cost.txt时使用scannerfile.hasnextint()的情况。cost.txt文件只包含double,因此第一次调用返回false,因此costgifts[]数组从不填充。
尝试改用scannerfile.hasnextdouble(),看看这是否能解决您的问题。

bqujaahr

bqujaahr2#

您扫描costs.txt文档的方式有一些问题。
文本中没有整型值,因此hasnextint()函数将始终返回false。
不是将total变量中的所有值相加,而是将从文件中读取的最后一个值输入到字符串变量中,此外,还需要一个整数值,因此字符串留空,这就是为什么在输出中看不到任何内容,而只看到上面提供的修复之后的最后一个值。
我把你的密码改成这样了
首先我把total的类型从string改为double
然后我改变了你的扫描区域:

while (scannerFile.hasNextInt()) {

            scannerFile.next();

            if(scannerFile.hasNextInt())

            {

                int i = scannerFile.nextInt();

                double cost = scannerFile.nextDouble();

                costGifts[i-1] = cost;

            }

            else
            {

                scannerFile.nextInt();

                total = scannerFile.nextLine();

            }

        }

        scannerFile.close();

对此:

int i = 0;
        while (scannerFile.hasNextDouble()) {

                double cost = scannerFile.nextDouble();
                costGifts[i] = cost;                    
                i++;                    
                total += cost;                                               

        }

        scannerFile.close();

    }

现在,当你阅读每一行的时候,总的计算是正确的,你也要确保你没有跳过任何一行。

相关问题