【问题标题】:File input stream in java - calculating datajava中的文件输入流 - 计算数据
【发布时间】:2013-03-06 03:13:42
【问题描述】:

我正在尝试将文件读入程序,然后获取 fileIn 对象并对它们进行计算以创建一个新变量,特别是读取价格并计算销售税。我以前做过这种类型的程序,我是按照下面的代码来做的。出于某种原因,这次我在运行程序时在控制台中收到此错误消息。

   Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at TaxCalculator.main(TaxCalculator.java:42)

到目前为止,我发现 .next() 方法给了我错误,因为我可以用 .nextLine(); 完整地阅读,但这违背了程序的目的。

    import java.io.FileInputStream;
    import java.util.Scanner;
    import java.io.FileNotFoundException;

    public class TaxCalculator {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner fileIn = null; //Initializes fileIn to empty

    //Declares variables
    String text;
    int value;
    double price;
    double tax;

    try 
    {
        //Attempt to open the file
        fileIn = new Scanner (
                new FileInputStream ("Basket.txt"));

        }
    catch (FileNotFoundException e)
    {
        //This block executed if the file is not found
        //then the program exits
        System.out.println("File not found.");
        System.exit(0);
    }



    //Read and print in lines
    value = fileIn.nextInt();
    text = fileIn.next();
    price = fileIn.nextDouble();
    tax = (price * .10);

    System.out.printf("%1d %2s %3.2f", value, text, price   );


    /**
    //Read and print next input line
    value = fileIn.nextInt();
    text = fileIn.next();
    price = fileIn.nextDouble();
    tax = (price * .10);    
    System.out.printf("%-7s %20s %22s %30.2f %n", value , text, text,
            price);
    //Read and print next input line
    value = fileIn.nextInt();
    text = fileIn.next();
    price = fileIn.nextDouble();
    tax = (price * .10);    
    System.out.printf("%-7s %18s %22s %30.2f %n", value , text, text,
            price);
    **/
    // Close file
    fileIn.close();

    System.out.println("\nEnd of Tax Calculator");


  }

 }

任何帮助将不胜感激,谢谢。 -编辑 - Basket.txt的内容

1 item at 10.49
1 special item at 13.99
1 candy bar at 0.75

Input 2:
1 imported pack of cigarettes at 10.00
1 imported bottle of alcohol at 44.50

Input 3:

1 imported bottle of alcohol at at 25.99
1 bottle of alcohol at 15.99
1 packet of cough drops at 4.99
1 box of imported cigarettes at 9.25

【问题讨论】:

  • 你能发布Basket.txt的内容吗?
  • 将 Basket.txt 的内容添加到帖子

标签: java java.util.scanner fileinputstream


【解决方案1】:

由 Scanner 抛出以指示检索到的令牌与预期类型的​​模式不匹配,或者令牌超出预期类型的​​范围。

这意味着你的程序试图读取一个不是整数的整数值。

【讨论】:

  • Scanner#next 返回一个String,可能没有更多的输入可以读取。
  • 堆栈跟踪表明这是一个 nextDouble() 方法调用。
  • 是这样的。我认为格式说明符实际上与扫描仪值之一不匹配。
【解决方案2】:

Basket.txt 包含带整数的字符串,

value = fileIn.nextInt(); text = fileIn.next(); price = fileIn.nextDouble();

读取前三行,所以前三行应该是 Integer String double 如果是字符串或混合字符串,它会显示

MismatchException

【讨论】:

  • 如果是这样的话,前三行不应该是intStringdouble吗?
  • 是的,程序不会跳过行来读取字符串后面的双精度数。
  • 如果我们想读取包含所有stringIntegerdouble的代码意味着我们可以使用try{value = fileIn.nextInt(); text = fileIn.next(); price = fileIn.nextDouble();}catch{}选项
猜你喜欢
  • 1970-01-01
  • 2011-06-06
  • 2012-05-16
  • 2013-03-07
  • 2018-05-25
  • 2018-07-11
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多