我在阅读第一个文件并翻译它或写入新文件时遇到了Java程序的问题。
以下是名为'transactions.txt'的第一个文本文件中的输入:
Joe Turing, 4124.50 D 200
Charles Babbage, 300.00 D 100
Sam Backus, 350.25 W 51
Sam Hopper, 600.50 X 600
Robert McCarthy, 1000.00 R 0
Jack Atanasoff, 200.00 W 200
Anne Stroustrup, 300.75 W 301
Amanda Hollerith, 500.00 W 2
我正在写一个名为“output.txt”的文件。我可以让我的程序读取和写入上面的第一行或上面的第二行。我需要它来写所有行。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class TranactionValidator {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner fileIn = null;
PrintWriter fileOut = null;
try {
fileIn = new Scanner(new FileInputStream("transactions.txt"));
}
catch (FileNotFoundException e) {
System.out.print("Input file not found!!");
System.exit(0);
}
try {
fileOut = new PrintWriter(new FileOutputStream("output.txt"));
}
catch (FileNotFoundException e) {
System.out.println("Cannot create file!");
System.exit(0);
}
while (fileIn.hasNextLine()) {
String firstName = fileIn.nextLine();
String lastName = fileIn.nextLine();
double beforeTransaction = fileIn.nextDouble();
String transactionType = fileIn.nextLine();
int transactionAmount = fileIn.nextInt();
fileOut.println(lastName + " " +firstName);
fileOut.println("Before Transaction: " + beforeTransaction);
fileOut.println("Transaction Type: " + transactionType);
if (transactionType.equals("W") || transactionType.equals("D") || transactionType.equals("R")){
switch (transactionType) {
case "W":
fileOut.println("Transaction Amount: " + transactionAmount);
if (transactionAmount <= -1) {
double afterTransactionW = beforeTransaction - transactionAmount;
fileOut.println("After Transaction: " + afterTransactionW);
if (afterTransactionW < 300.00) {
fileOut.println("Warning! Account balance below $300.00");
}
}
else {
fileOut.println("Number must be positive");
}
break;
case "D":
case "d":
fileOut.println("Transaction Amount: " + transactionAmount);
if (transactionAmount <= -1) {
double afterTransactionD = beforeTransaction + transactionAmount;
fileOut.println("After Transaction: " + afterTransactionD);
if (afterTransactionD < 300.00) {
fileOut.println("Warning! Account balance below $300.00");
}
}
else {
fileOut.println("Number must be positive");
}
break;
case "R":
case "r":
fileOut.println("Transaction Amount: " + transactionAmount);
if (transactionAmount <= -1) {
double afterTransactionR = beforeTransaction;
fileOut.println("After Transaction: " + afterTransactionR);
if (afterTransactionR < 300.00) {
fileOut.println("Warning! Account balance below $300.00");
}
}
else {
fileOut.println("Number must be positive");
}
break;
default:
fileOut.println("After Transaction: Error");
break;
}
}
else {
fileOut.println("Must enter a code such as W, R, and D");
}
fileOut.println("------------------------------------------------------------------------");
fileOut.close();
}
fileIn.close();
System.exit(0);
}
}
当我运行程序时,我会在控制台中抛出这个之前得到几个“无法创建文件!!”:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at TranactionValidator.main(TranactionValidator.java:34)
有人能指出我正确的方向吗?
答案 0 :(得分:0)
错误InputMismatchException
的原因是您尝试分配给String
以及double
和int
这条线:
String firstName = fileIn.nextLine();
String lastName = fileIn.nextLine();
double beforeTransaction = fileIn.nextDouble();// line 34
...
但你可以试试这样的事情:
while (fileIn.hasNextLine()) {
String[] arr = fileIn.next().split(" ");
String firstName = arr[0];
String lastName = arr[0];
double beforeTransaction = Double.parseDouble(arr[0]);
String transactionType = arr[0];
int transactionAmount = Integer.parseInt(arr[0]);