2013-03-06 91 views
1

我想读入一个文件到程序中,然后拿fileIn对象并对它们进行计算来创建一个新变量,具体是读取价格并计算销售税。之前我已经完成了这种类型的程序,而且我的代码如下所示。出于某种原因,这次我在控制台中得到这个错误信息,当我运行该程序时。在java中的文件输入流 - 计算数据

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 
+3

你能发布'Basket.txt'内容吗? – 2013-03-06 03:15:04

+0

添加basket.txt的内容 – 2013-03-06 22:58:57

回答

1
通过

Scanner抛出的 内容以指示检索到的令牌不期望类型匹配的模式,或者该标记超出范围的预期的类型。

这意味着您的程序尝试读取的值不是整数。

+0

'Scanner#next'返回一个'String',也许没有更多的输入来读取。 – 2013-03-06 03:22:19

+0

堆栈跟踪提供它是一个nextDouble()方法调用。 – 2013-03-06 03:50:35

+0

这是沿着这些线。我认为格式说明符实际上与其中一个扫描仪值不匹配。 – 2013-03-07 20:01:40

0

Basket.txt包含字符串与整数,

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

读取前三行,所以第一个三线应该在整数字符串如果是字符串或混合串,它会显示

MismatchException 
+0

如果情况如此,前三行不应该是“int”,“String”还是“double”? – 2013-03-06 03:56:00

+0

你对..我已经更新 – kark 2013-03-06 04:03:42

+1

是的,程序不会跳过线读取在一个字符串后面的双。 – 2013-03-07 20:02:31