首页 / JAVA / java-输入不匹配异常?
java-输入不匹配异常?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-输入不匹配异常?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2008字,纯文字阅读大概需要3分钟。
内容图文
我对Java还是很陌生.我为上课创建了该程序,它给了我一个从未有过的错误.如果有人可以帮助那将是很好.谢谢!
import java.util.Scanner;
import java.io.*;
public class grades {
public static void main(String[] args) throws IOException {
// Define file names
final String INPUT_FILE = "gradesinput.txt";
final String OUTPUT_FILE = "gradesoutput.txt";
// define variables
int grade;
String name = null, filename;
double value = 0;
String msg;
// Access the input/output files
File inputDataFile = new File(INPUT_FILE);
Scanner inputFile = new Scanner(inputDataFile);
FileWriter outputDataFile = new FileWriter(OUTPUT_FILE);
PrintWriter outputFile = new PrintWriter(outputDataFile);
System.out.println("Reading file " + INPUT_FILE + "\r\n" +
"Creating file " + OUTPUT_FILE);
// Read all of the values from the file
while (inputFile.hasNext()) {
grade = inputFile.nextInt();
name = inputFile.nextLine();
name = name.trim();
} // End while
if(value >= 90)
{
msg = "OUTSTANDING";
}
else if (value >= 70)
{
msg = "Satisfactory";
}
if(value >= 90){
msg = "OUTSTANDING";
}else{
if(value >= 70){
msg = "Satisfactory";
}else
msg = "FAILING";
}
outputFile.println(value + " " + name + " " + msg);
outputFile.println("processed names");
outputFile.println("between 70 and 89 inclusive");
outputFile.close();
} // End outputResults
} // End class
我收到此错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at grades.main(grades.java:37)
解决方法:
错误在这里:grade = inputFile.nextInt();
您正在尝试读取一个int,但是该文件在此位置没有int.
Scans the next token of the input as an int. This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.
内容总结
以上是互联网集市为您收集整理的java-输入不匹配异常?全部内容,希望文章能够帮你解决java-输入不匹配异常?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。