【问题标题】:Not Printing desire output using arrays and reading from files不使用数组打印期望输出并从文件中读取
【发布时间】:2018-12-06 18:59:21
【问题描述】:

程序功能是从两个单独的文本文件中读取,一个文本文件称为gift(随机名称)和cost(每天的总成本)。如果用户输入为 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: $

gifts.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

【问题讨论】:

  • 那么想要的输出是什么?
  • 当日成本为 0,但我的文本文件有 12 个数字,总成本不显示意味着将所有 12 个数字相加等于 12 天的总成本。

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


【解决方案1】:

您扫描 cost.txt 文档的方式存在一些问题。

text中没有Integer类型的值,所以hasNextInt()函数总会给你返回false。

您不是将总变量中的所有值相加,而是输入从文件中读取的最后一个值到 String 变量中,此外,您还期待一个 Integer 值,因此 String 保留为空,这就是为什么您没有在输出中看到任何内容,并且只看到上面提供的修复后的最后一个值。

我这样修改了你的代码

首先我将 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();

    }

现在,在您阅读每一行时,总数已正确计算,并且您还确保没有跳过任何行。

【讨论】:

  • 感谢它的工作,我一直在为这个项目苦苦挣扎。干杯
【解决方案2】:

问题是您在评估 cost.txt 时使用了scannerFile.hasNextInt()。文件 cost.txt 仅包含双精度,因此第一次调用返回 false,因此永远不会填充 costGifts[] 数组。

尝试改用scannerFile.hasNextDouble(),看看是否能解决您的问题。

【讨论】:

  • 它确实解决了这个问题,但它打印的最后一个数字是这种情况下 39094.93 并且总数也没有显示
  • 不使用 int i =scannerFile.nextInt(),而是在 while 之外初始化一个从 0 开始的变量,并在添加每个值后增加它。这也将消除使用 i-1 作为索引的需要。如果它是您问题的正确解决方案,请务必接受它作为答案。
  • 我还是有同样的问题。 D:
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 2023-03-19
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
相关资源
最近更新 更多