0

Please look at the code below. I am getting InputMismatchExeption error and I am not sure why. I think that my data types are correct. First I have a writer class where I create a file with input and than the reader class where I am trying to read the info from the file created in the writer class.

My writer class:

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();
    }
}

My Reader Class:

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();
    }
}

I am getting the following error:

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 pretty sure that my data types are correct, where is the error coming from and how do I fix it?

Gerold Broser
  • 11,355
  • 4
  • 36
  • 85

2 Answers2

0

You're trying to parse a String into an int. "Learn about mobile application development, market opportunities, and technical requirements for Apple (iOS), Google (Android) and Microsoft (Mobile 8)." isn't an int but a String.

On this line: int courseLimit = input.nextInt();

You could fix it like this:

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();
    }

For more information on why this happens, you can take a look at this question.

Community
  • 1
  • 1
0

I am getting InputMismatchExeption error and I am not sure why?

The error occurs during your while loop, the two lines below is causing the problem.

int courseId = input.nextInt();
int courseLimit = input.nextInt();

I recommend to use Scanner.hasNextInt() method first to confirm that it is actually an int data type before assigning it to the variable.

Example:

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);
    }

NOTE - This will append 0 to the output if there is no integer value found within the data you're reading.

Ousmane D.
  • 50,173
  • 8
  • 66
  • 103