嘿大家我正在做一个项目并且遇到一点麻烦我收到了java.util.InputMismatchException任何帮助都会非常感激。我真的很喜欢用java编程,所以请原谅我,如果我犯了一个明显的愚蠢错误。该程序的目的是读取一个文件获取前三行用于6个不同的变量(三个整数和三个字符串)然后读取文件的其余部分,格式化,然后做一些数学然后把它放到一个新的文件。我从中获取数据的文件格式如下。
法院50
专栏10
Club 25
Rascal Conway 10 Box
Loretta Barrett 3 Court
Loras Tyrell 5俱乐部
Margaery Tyrell 8 Box。
以下是代码:
public class ProjectTicket {
public static void main(String[] args) throws IOException {
/////////////////////// amount of tickets person would buy
int amount;
/////////////////////// the tickets prices
int tprice1 = 0;
int tprice2 = 0;
int tprice3 = 0;
//////////////////////the ticket types
String ticket1 = null;
String ticket2 = null;
String ticket3 = null;
////////////////////// the total price
int price = 0;
////////////////////// customers name
String firstName;
String lastName;
////////////////////// name of seat type
String seat;
PrintWriter outputFile = new PrintWriter("portland2out.txt");
File file = new File("portlandvip2.txt");
Scanner inputFile = new Scanner(file);
////////////////////// getting ticket name and ticket price
for (int i = 0; i < 2; i++) {
ticket1 = inputFile.next();
tprice1 = inputFile.nextInt();
ticket2 = inputFile.next();
tprice2 = inputFile.nextInt();
ticket3 = inputFile.next();
tprice3 = inputFile.nextInt();
outputFile.println(ticket1 + " " + "$" + tprice1);
outputFile.println(ticket2 + " " + "$" + tprice2);
outputFile.println(ticket3 + " " + "$" + tprice3);
}
////////////////////// getting customer name,number of tickets, type of ticket
while (inputFile.hasNext()) {
firstName = inputFile.next();
lastName = inputFile.next();
amount = inputFile.nextInt();
seat = inputFile.next();
////////////////////////////////// doing math for final billing.
if (seat.equals(ticket1))
price = tprice1 * amount;
else if (seat.equals(ticket2))
price = tprice2 * amount;
else if (seat.equals(ticket3))
price = tprice3 * amount;
///////////////////////////////// printing in format to file
outputFile.printf("%-10s%-10s$%,.2f\n", firstName, lastName, price);
}
inputFile.close();
}
}
这是错误:
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at BlancovichProject2.main(BlancovichProject2.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
我不确定问题是什么。我一直在环顾四周,当检索到的信息不是它正在寻找的时候,似乎给出了错误类型。再次感谢您的任何帮助!
答案 0 :(得分:0)
您正在尝试使用
ticket1 = inputFile.next();
tprice1 = inputFile.nextInt();
对于看起来像Rascal Conway 10 Box
的数据,因此您尝试将Conway
读为引发InputMismatchException
的整数。
答案 1 :(得分:0)
它可以工作,但你只需要迭代一次或取消forloop
更改:强>
for (int i = 0; i < 2; i++) {
ticket1 = inputFile.next();
tprice1 = inputFile.nextInt();
ticket2 = inputFile.next();
tprice2 = inputFile.nextInt();
ticket3 = inputFile.next();
tprice3 = inputFile.nextInt();
outputFile.println(ticket1 + " " + "$" + tprice1);
outputFile.println(ticket2 + " " + "$" + tprice2);
outputFile.println(ticket3 + " " + "$" + tprice3);
}
到此:
ticket1 = inputFile.next();
tprice1 = inputFile.nextInt();
ticket2 = inputFile.next();
tprice2 = inputFile.nextInt();
ticket3 = inputFile.next();
tprice3 = inputFile.nextInt();
outputFile.println(ticket1 + " " + "$" + tprice1);
outputFile.println(ticket2 + " " + "$" + tprice2);
outputFile.println(ticket3 + " " + "$" + tprice3);
此外,您对printf的格式错误。要验证结果注释
//outputFile.printf("%-10s%-10s$%,.2f\n", firstName, lastName, price);
并添加此项以验证您是否具有正确的输出:
System.out.printf("%-10s%-10s$%,d\n", firstName, lastName, price);
然后如果他们是对的......你可以将它输出到你的文件..