Scanner is skipping nextLine() after using next() or nextFoo()?
(21个回答)
3年前关闭。
请查看下面的代码。我收到
InputMismatchExeption
错误,我不确定为什么。我认为我的数据类型是正确的。首先,我有一个writer类,在其中我用输入创建一个文件,而不是Reader类,在其中尝试从writer类中创建的文件中读取信息。
我的作家类:
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)
我很确定自己的数据类型正确,错误来自哪里以及如何解决?
I am getting InputMismatchExeption error and I am not sure why?
在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。
我是一名优秀的程序员,十分优秀!