プログラムにファイルを読み込んでから、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)
私がこれまでに見つけたのは、.nextLine();で全行を読み取ることができるため、.next()メソッドでエラーが発生することですが、これはプログラムの目的に反します。
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