3

So I'm trying to compute a Simple program that has an n number of objects of the type Person and each person has a name, id, passport and driving licence.

It goes like this:

public class Person {

    private String name;
    private int id;
    private int passport;
    private int licence;

    Person(String name, int id, int passport, int licence) {
        this.name=name;
        this.id=id;
        this.passport=passport;
        this.licence=licence;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner jin=new Scanner(System.in);
        int n=jin.nextInt(), i;
        Person[] dudes=new Person[n];
        for(i=0; i<n; i++) {
            String name=jin.nextLine();
            int id=jin.nextInt();
            int passport=jin.nextInt();
            int licence=jin.nextInt();
            dudes[i]=new Person(name, id, passport, licence);
        }
    }
}

However when I enter a value for the String name it gives me this error:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at test.Main.main(Main.java:20)

Any ideas?

TimoStaudinger
  • 34,772
  • 13
  • 76
  • 86
David Mathers
  • 153
  • 1
  • 7

5 Answers5

2

The same problem is already discussed here. So, the reason you get such exception is as follow:

  1. int n = scanner.nextInt(), i; you enter a number then press Enter. nextInt() consumes only the number, not the end of line.
  2. When you entered a number and pressed Enter the following line: String name=jin.nextLine(); executes automatically.
  3. You are expecting a String type but its already this line: int id=jin.nextInt();

That's why you get exception. You can avoid it without modification if you are looping once and enter for instance as follow:

  1. 1 test
  2. 1
  3. 2
  4. 3

But better to read as nextLline() and parse your integer as Integer.parseInt(nextLine)

Ibrokhim Kholmatov
  • 1,280
  • 2
  • 10
  • 16
1

May not be the right approach but, try with default constructor and making the for loop as

for(i=0; i<n; i++)
{
    dudes[i]=new Person();
    dudes[i].name = jin.nextLine();
    dudes[i].id = jin.nextInt();
    dudes[i].license = jin.nextInt();
    dudes[i].passport= jin.nextInt();   
}
Prasanna Kumar
  • 350
  • 1
  • 4
  • 13
1

If you change String name = jin.nextLine(); to String name = jin.next(); it works how you have it.

An easier way would be to clear the buffer after typing an int like so for first and last names.

for(i=0; i<n; i++)
    {
        jin.nextLine();  // add this to clear the input from before.
        String name=jin.nextLine();
        int id=jin.nextInt();
        int passport=jin.nextInt();
        int licence=jin.nextInt();

        dudes[i]=new Person(name, id, passport, licence);
    }
  • That did help but what if i want the name to be the name and the last name(the name to be a sentence instead of a single word)? – David Mathers Nov 29 '17 at 20:47
  • I edited my answer and added a way to do that as well. – Reginol_Blindhop Nov 29 '17 at 20:54
  • Someone can correct me if I am wrong but I think nextInt() doesn't clear the newline character and only grabs the number so when you call nextLine() it searches for the first newline which is leftover. so if you call nextLine() it should clear that and have it how you want for next time. – Reginol_Blindhop Nov 29 '17 at 20:58
1

The problem for that in this section of code:

int n=jin.nextInt(), i;
        Person[] dudes=new Person[n];
        for(i=0; i<n; i++) {
            String name=jin.nextLine();
            int id=jin.nextInt();
            int passport=jin.nextInt();
            int licence=jin.nextInt();
            dudes[i]=new Person(name, id, passport, licence);
        }

Actually when you read input n=jin.nextInt() the cursor of scanner which reads input data will still in the same line so when compiler go to this line String name=jin.nextLine() to execute it, it will read only the string at the same line and in your case you press Enter after reading n then you enter name string so you will read the integer variable id instead of string variable name and the compiler will throw error Msg java.util.InputMismatchException

To understand the problem try these cases :

int n=jin.nextInt();
String name=jin.nextLine();
int id=jin.nextInt();
int passport=jin.nextInt();
int licence=jin.nextInt();
System.out.println(name + " " + name.length());

enter image description here

in this case scanner will read name string after entering n integer at the same line.

enter image description here

but in this case scanner will not read name string after entering n integer because you hit Enter and go to the next line so name variable will be empty string.

To solve it you only need to put: String name=jin.next();

Community
  • 1
  • 1
Oghli
  • 1,585
  • 1
  • 9
  • 29
0

Why don't you try something like this, or do you have to use a simple array? Also add setter and getter methods to your Person class. If you extend your Program, you can loop over your ArrayList and print out all the persons in it.

    Scanner jin = new Scanner(System.in);
    List<Person> dudes = new ArrayList<>();

    System.out.print("How many persons do you wan't to add?");
    int n = jin.nextInt();

    for (int i = 0; i < n; i++) {

        jin.nextLine();
        System.out.print("Enter your name: ");
        String name = jin.nextLine();
        System.out.print("Enter your id: ");
        int id = jin.nextInt();
        System.out.print("Enter your passport:");
        int passport = jin.nextInt();
        System.out.println("enter your licence:");
        int licence = jin.nextInt();
        Person newPerson = new Person(name, id, passport, licence);
        dudes.add(newPerson);

        System.out.println("new person added: " + newPerson.getName() + " Id: " + newPerson.getId() + " passport: "
                + newPerson.getPassport() + " licence: " + newPerson.getLicence());
    }

    //prints your ArrayList
    for (Person person : dudes) {
        System.out.println("name: " + person.getName() + " Id: " + person.getId() + " passport: "
                + person.getPassport() + " licence: " + person.getLicence());
    }
TheQuestioner
  • 242
  • 1
  • 5
  • 10