我想读入一个文件到程序中,然后拿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
你能发布'Basket.txt'内容吗? – 2013-03-06 03:15:04
添加basket.txt的内容 – 2013-03-06 22:58:57