user2387220 user2387220 - 4 months ago 14
Java Question

Reading textfile with Scanner results in 'InputMismatchException'

I have text file has in each line date of person (Name, gender, age, weight)


David Smith

Male

32

85.83

Sarah Apple

Female

27

56.23

Teller Saimone

Male

29

87.71


Here is my code:

Scanner inputFile = new Scanner(myFile);
while (inputFile.hasNext()) {
Persone Pers = new Persone();
Pers.setVehicleMake(inputFile.nextLine());
System.out.println(Pers.getVehicleMake());
Pers.setVehicleModel(inputFile.nextLine());
System.out.println(Pers.getVehicleModel());
Pers.setNumberCylinders(inputFile.nextInt());
System.out.println(Pers.getNumberCylinders());
Pers.setEstMPG(inputFile.nextDouble());
System.out.println(Pers.getEstMPG());
auotList.add(Pers);
}


When I run the code I got this error:


run:

David Smith

Male

32

85.83

Sarah Apple

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 PersonDrive.main(PersonDrive.java:36)

/home/jaguar/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1

BUILD FAILED (total time: 1 second)


It seem when it do the loop for the next read it read whitespace

Answer

Using parseInt() and parseDouble() methods in combination with nextLine() method will solve your problem. I have written some code:

public class Person{
    private String name;
    private String gender;
    private int age;
    private double weight;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }

}

and

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class PersonTest {   
    public static void main(String[] args) throws FileNotFoundException {
        File inputFile = new File("data.txt");
        Scanner sc = new Scanner(inputFile);

        ArrayList<Person> peopleList = new ArrayList<Person>();
        Person p;

        while (sc.hasNext()){
            p = new Person();
            p.setName(sc.nextLine());
            System.out.println(p.getName());
            p.setGender(sc.nextLine());
            System.out.println(p.getGender());
            p.setAge(Integer.parseInt(sc.nextLine()));
            System.out.println(p.getAge());
            p.setWeight(Double.parseDouble(sc.nextLine()));
            System.out.println(p.getWeight());
            peopleList.add(p);
        }

        sc.close();
    }
}

I think the problem why your code didn't work is that after nextDouble() found 85.83, the scanner skipped that number and was still at 4th line. When you called nextLine() in the second loop, it returned the rest of 4th line, which was blank. Using my solution, you can take a full line and then easily convert it to integer number or double number.