【问题标题】:When reading input from a File, not all my the lines in the file are read in Java?从文件读取输入时,不是文件中的所有行都是用 Java 读取的?
【发布时间】:2014-10-22 00:04:09
【问题描述】:

所以对于作业,我必须从文件中读取输入,然后将其输出。 文件中的信息格式如下:第一行是股票代码,下一行是购买的数量。 例如我们将使用的文件:

.ab
15
mox
16
.fy
8
mixe
34

然后我必须输出每个符号的计数和总价格。 但不知何故,当我运行它时,它只读取前 3 组数据——这个例子只有 3 行数据而不是 4 行。 例如。输出将是:

Enter filename ... lab5data.txt
00000 54.05
00002 Symbol mox does not exist
00003 10.70
00004 Symbol mixe does not exist

而且我的代码不会输出最后一行“00004 Symbol mixe doesn't exist”

这是我的代码:

import java.util.Scanner;
import java.io.PrintStream;
import type.lib.Stock;
import java.io.File;
import java.text.DecimalFormat;
public class Check05B
{
    public static void main(String[] args) throws java.io.IOException
    {
        Scanner input = new Scanner(System.in);
        PrintStream output = new PrintStream(System.out);
        output.print("Enter filename ... ");
        String fileName = input.nextLine();
        Scanner fileInput = new Scanner(new File(fileName));
        double totalValue = 0;
        int count = 00000;
        String symbol = fileInput.next();
        int quantity = fileInput.nextInt();
        while (fileInput.hasNext())
        {
            Stock myStock = new Stock(symbol);
            double total = myStock.getPrice() * quantity;
            String aFormatCounter = new DecimalFormat("00000").format(count);
            if (myStock.getName() == null)
            output.println(aFormatCounter + " Symbol " + symbol + " does not exist!");
            else
            output.printf("%s %.2f%n", aFormatCounter, myStock.getPrice());
            totalValue += total;
            symbol = fileInput.next();
            count++;
            quantity = fileInput.nextInt();
            count++;
        }
        output.printf("Total value = %.2f%n", totalValue);
        fileInput.close();
    }
}

有人可以帮忙吗?我怎样才能让它读取所有的行?谢谢!!

【问题讨论】:

  • 您需要在问题中包含Stock 类的代码。

标签: java file-io


【解决方案1】:

您需要在循环顶部读取symbolquantity

考虑

    while (fileInput.hasNext())
    {
        String symbol = fileInput.next();
        if (fileInput.hasNextInt () == false) {
            System.err.println ("File Format Error - expecting an int");
            break;
        }    
        int quantity = fileInput.nextInt();
        Stock myStock = new Stock(symbol);
        double total = myStock.getPrice() * quantity;
        String aFormatCounter = new DecimalFormat("00000").format(count);
        if (myStock.getName() == null) {
            output.println(aFormatCounter + " Symbol " + symbol + " does not exist!");
        }
        else {
            output.printf("%s %.2f%n", aFormatCounter, myStock.getPrice());
        }
        totalValue += total;
        count = count + 2;
    }

【讨论】:

  • 是的,解决了它!太感谢了!! :) 我会“竖起大拇指”你的帖子,但我还没有足够的声望点!再次感谢!!
  • 您是否会考虑仔细检查,例如 while(hasNext) { if (hasNextInt) { ... 或者只是过于挑剔并且超出了问题的范围;)
  • @MadProgrammer not nit picky but beyond the scope of the question 怎么样。但说真的,我会添加它。
  • @ScaryWombat 虽然 +1 只是一个过客的想法 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多