0

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

Tom
  • 14,120
  • 16
  • 41
  • 47
user2387220
  • 305
  • 2
  • 3
  • 7
  • Good about your question: you provide code, input and expected output an error message. Bad about your question: it has been asked here a zillion times. It would have been much quicker for you to do some prior research, instead of typing down content that comes in on almost daily basis ... – GhostCat Jul 14 '16 at 08:02
  • Btw you shouldn't clone a `Car` class and just rename it `Person`, the resulting code will then be confusing, when you keep the old method names. – Tom Jul 14 '16 at 08:02
  • You have over 2000 lines of code if I am not totally mistaken. Surely this is not your whole program. Anyways, `nextLine()` returns a `boolean`. You are probably expecting it to return a `String`. – RaminS Jul 14 '16 at 08:04
  • @Gendarme *"`nextLine()` returns a `boolean`"* It doesn't and it wouldn't make sense. And your first sentence is also wrong. `Scanner` is not OPs class. – Tom Jul 14 '16 at 08:06
  • @Tom Yep. I used ctrl+F in the JavaDocs and landed at `hasNextLine()` and thought I was at `nextLine()`. Doesn't `at java.util.Scanner.nextInt(Scanner.java:2076)` mean that he is calling `nextInt()` on line 2076? – RaminS Jul 14 '16 at 08:10
  • @Gendarme Yes he does, but `Scanner` is *not* his class. It is part of the Java installation and provided by it. OP just uses it. His part in the Stacktrace is *"at PersonDrive.main(PersonDrive.java:36)"* and this is where he calls `nextInt`, the remain Stacktrace lines are `Scanner` internals. – Tom Jul 14 '16 at 08:14
  • Thanks N.Hung it solved my issue – user2387220 Jul 14 '16 at 09:18

1 Answers1

1

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.

N.Hung
  • 144
  • 6