Question

So i had this program running yesterday and accidentally ran the debugger console in netbeans IDE 7.4 and ever since the program stopped working. I don't know what it changed within my code.

Here is the error:

Petname: 
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at veterinarian.Veterinarian.main(Veterinarian.java:107)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

I added a simple println statement to see what is stored and its null that's the petname.

The code is:

public static void main(String[] args) {
    boolean pick = false;       
//***************************add file*******************************************
File file = new File("VetList.txt");      
//*********************declare ojbects******************************************
try{
        Scanner read = new Scanner(file);
        int fix = read.nextInt();
        do{
        for(int x = 0; x < fix; x++){
            String petname = read.nextLine();
            System.out.println("Petname: " + petname);
            int birthday = read.nextInt();
            String species = read.next();
            double bill = read.nextDouble();
            String owner = read.next();
            Veterinarian work = new Veterinarian(petname, birthday, species, bill, owner);
            Veterinarian.Vet.add(work);
            }
    }
    while(read.hasNextLine() == pick);
    }  
catch(FileNotFoundException FNF){
    System.err.format("File not found");}

The txt file is:

8
hopper
2003
kangaroo
555
Melody_harper
ketty
2009
cat
44
Kitty_katz
Spot
2005
Dog
333
Dottie_Marks
Johnny
2008
cat
111
Misty_waters
Stripper
2011
sckunk
432 
Oddie_sweet
Edward
1995
Horse
630
Frieda_gelding
Casper
1998
snake
88
Bo_constricter
boots
2005
Horse
987
Ida_Shuman

I cannot change the file format to read better.

Just to note, i have tried to restore the java file itself to a previous time before this incident and it still will not run and gives the same error. I also re-installed Netbeans.

Was it helpful?

Solution

For debugging if you change all your datatypes to String and change the code to something like this

                String petname = read.nextLine();
                System.out.println("Petname: " + petname);

                String birthday = read.next();
                String species = read.next();
                String bill = read.next();
                String owner = read.next();
                System.out.println("BirthDay" + birthday);
                System.out.println("Name"+petname);
                System.out.println("BIll" + bill);
                System.out.println("Speci" + species);
                System.out.println("Owner " + owner);

You should get an output like

 Petname: 
 BirthDayhopper
 Name
 BIllkangaroo
 Speci2003
 Owner 555

That means your birthday is a string but you are trying to store as an int.. Thats why its giving the exception. If you change the code to following

 if (read.hasNext()) {
  //if prevents NoSuchElementException
                    String petname = read.next();
                    System.out.println("Petname: " + petname);

                    int birthday = read.nextInt();
                    String species = read.next();
                    double bill = read.nextDouble();
                    String owner = read.next();
                    System.out.println("BirthDay" + birthday);
                    System.out.println("Name" + petname);
                    System.out.println("BIll" + bill);
                    System.out.println("Speci" + species);
                    System.out.println("Owner " + owner);
                }

You get the desired output and the datatypes you wanted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top