请查看下面的代码。我收到InputMismatchExeption
错误,我不知道为什么。我认为我的数据类型是正确的。首先,我有一个编写器类,我在其中创建一个带输入的文件,而不是读者类,我试图从编写器类中创建的文件中读取信息。
我的作家班:
public class WriteData {
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File("courses.txt");
if (file.exists()) {
System.out.println("File already exists");
System.exit(0);
}
java.io.PrintWriter output = new java.io.PrintWriter(file);
output.println(101);
output.println("April 03, 2017 to May 07, 2017");
output.println("Introduction to Mobile Application Design and Development");
output.println("Learn about mobile application development, market opportunities, and technical requirements for Apple (iOS), Google (Android) and Microsoft (Mobile 8).");
output.println(30);
output.println(102);
output.println("April 03, 2017 to May 07, 2017");
output.println("Advanced Mobile Application Design and Development");
output.println("This class will build on the knowledge presented in the introductory course and focus on key topics relevant across implementation platforms.");
output.println(30);
output.println(103);
output.println("April 03, 2017 to May 07, 2017");
output.println("Mobile Development for Apple iPhone and iPad Applications");
output.println("Design and develop applications for the Apple iPhone and iPad. Recap how to register for the development program, download, and install XCode.");
output.println(30);
output.println(104);
output.println("April 03, 2017 to May 07, 2017");
output.println("Advanced Mobile Development for Apple iPhone and iPad");
output.println("This advanced course focuses on more complex User Interface Design and Navigation concepts, the Core Data Model, Data Persistence in XML and SQLite database, programmatic handling of user configuration data and property lists");
output.println(30);
output.println(105);
output.println("April 03, 2017 to May 07, 2017");
output.println("Introduction to Android Application Development");
output.println("Understand Google's Android mobile device platform, its position in the marketplace and the applications for actual devices. Receive hands-on experience using Google's Android Software Development Kit (SDK).");
output.println(30);
output.println(106);
output.println("April 03, 2017 to May 07, 2017");
output.println("Advanced Application Development for Android");
output.println("Gain advanced knowledge of the Android platform including: issues and techniques, structuring applications for efficiency and reliability, accessing Web Services and integrating with 3rd party libraries.");
output.println(30);
output.println(107);
output.println("April 03, 2017 to May 07, 2017");
output.println("Java Programming I");
output.println("Java is an excellent choice for those new to programming, looking to enhance their current skill set or change their career. The aim of this course is to provide students with the knowledge and experience to write and design sophisticated programs.");
output.println(30);
output.println(108);
output.println("April 03, 2017 to May 07, 2017");
output.println("Java Programming II");
output.println("Expand your knowledge of Java and learn about several of the advanced features available in the Java programming environment.");
output.println(30);
output.println(109);
output.println("April 03, 2017 to May 07, 2017");
output.println("Agile Development");
output.println("Agile techniques are rapidly becoming integral parts of the project management process and are often implemented to motivate cultural shifts required to stimulate innovation and improved operational efficiency.");
output.println(30);
output.println(110);
output.println("April 03, 2017 to May 07, 2017");
output.println("Creating Websites for Mobile Devices");
output.println("This course looks at designing and developing web sites specifically for mobile devices.");
output.println(30);
output.close();
}
}
public class ReadData {
public static void main(String[] args) throws FileNotFoundException, Exception {
java.io.File file = new java.io.File("courses.txt");
Scanner input = new Scanner(file);
while(input.hasNext()) {
int courseId = input.nextInt();
String datesOfCourse = input.nextLine();
String courseName = input.nextLine();
String courseDescription = input.nextLine();
int courseLimit = input.nextInt();
System.out.println("Course ID: " + courseId + ", " + "Dates of course: " + datesOfCourse + ", " +
"Name of Course: " + courseName + ", " + "Course Description: " + courseDescription + ", " +
"Course Limit: " + courseLimit);
}
input.close();
}
}
我收到以下错误:
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 ReadData.main(ReadData.java:13)
我很确定我的数据类型是正确的,错误来自何处以及如何解决?
答案 0 :(得分:0)
您正在尝试将String解析为int。 “了解Apple(iOS),Google(Android)和Microsoft(Mobile 8)的移动应用程序开发,市场机会和技术要求。”不是int而是String。
在这一行:int courseLimit = input.nextInt();
你可以这样解决:
public class ReadData {
public static void main(String[] args) throws FileNotFoundException, Exception {
java.io.File file = new java.io.File("courses.txt");
Scanner input = new Scanner(file);
while(input.hasNext()) {
int courseId = input.nextInt();
input.nextLine();
String datesOfCourse = input.nextLine();
String courseName = input.nextLine();
String courseDescription = input.nextLine();
int courseLimit = input.nextInt();
System.out.println("Course ID: " + courseId + ", " + "Dates of course: " + datesOfCourse + ", " +
"Name of Course: " + courseName + ", " + "Course Description: " + courseDescription + ", " +
"Course Limit: " + courseLimit);
}
input.close();
}
有关发生这种情况的详细信息,请查看this question。
答案 1 :(得分:0)
我收到InputMismatchExeption错误,我不确定为什么?
在while循环期间发生错误,下面两行导致问题。
int courseId = input.nextInt();
int courseLimit = input.nextInt();
我建议首先使用Scanner.hasNextInt()
方法确认它在将数据分配给变量之前确实是一个int数据类型。
示例:
while(input.hasNext()) {
int courseId = 0;
if(input.hasNextInt()) courseId = input.nextInt();
input.nextLine();
String datesOfCourse = input.nextLine();
String courseName = input.nextLine();
String courseDescription = input.nextLine();
int courseLimit = 0;
if(input.hasNextInt()) courseLimit = input.nextInt();
System.out.println("Course ID: " + courseId + ", " + "Dates of course: " + datesOfCourse + ", " +
"Name of Course: " + courseName + ", " + "Course Description: " + courseDescription + ", " +
"Course Limit: " + courseLimit);
}
注意 - 如果在您正在阅读的数据中找不到整数值,则会将0附加到输出。