0

I am curious as to what the error java.util.InputMismatchException means and why I am getting it. This program is the driver class for a file called Dog.java that takes a dog's info and stores it as a string. This file is what directly takes the info and later prints the information out.

The data I am putting in is:

Disco Bandito  
4  
Sally Struthers  
5  
Moosen  
87  

Here is my Code:

import java.util.Scanner;

public class Kennel {

    public static void main(String[] args) {

    String value1 = null;
    int value2 = 0;
    String value3 = null;
    int value4 = 0;
    String value5 = null;
    int value6 = 0;

        //takes the input from a text file

    Scanner scanner = new Scanner(System.in);

        while (scanner.hasNextLine()){

        value1 = scanner.nextLine();
        value2 = scanner.nextInt();
        value3 = scanner.nextLine();
        value4 = scanner.nextInt();
        value5 = scanner.nextLine();
        value6 = scanner.nextInt();
    }

        //the three "dogs" in a kennel

    Dog Dog1 = new Dog();

    Dog1.setName(value1);
    Dog1.getName();
    Dog1.setAge(value2);
    Dog1.getAge();
    Dog1.toString();

    Dog Dog2 = new Dog();

    Dog2.setName(value3);
    Dog2.getName();
    Dog2.setAge(value4);
    Dog2.getAge();
    Dog2.toString();

    Dog Dog3 = new Dog();

    Dog3.setName(value5);
    Dog3.getName();
    Dog3.setAge(value6);
    Dog3.getAge();
    Dog3.toString();

    System.out.println(Dog1.toString());
    System.out.println(Dog2.toString());
    System.out.println(Dog3.toString());

    }
}

Thanks, Jake

daniel gratzer
  • 52,833
  • 11
  • 94
  • 134
  • ***Related Question:*** [Scanner issue when using nextLine after nextInt](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextint) – Eng.Fouad Nov 16 '12 at 00:51

1 Answers1

1

From the docs for InputMismatchException:

Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

That pretty much says all that needs to be said. Check your file for unexpected input.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • +1 - though "read the javadoc" also says pretty much all that needs to be said ... but more succinctly :-) – Stephen C Nov 16 '12 at 01:15
  • @StephenC - Well, yes. Answering RTFM was my first instinct. But there is some value in posting the essential info here, along with a constructive suggestion for what to do about it. – Ted Hopp Nov 16 '12 at 02:15