I can't seem to debug these run-time errors. help would be appreciated.

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at lab2.readFile(lab2.java:92)
    at lab2.main(lab2.java:79)
import java.io.*;
import java.util.*;

class Customer {

int account_id;
char[] ch1 = new char[20];
String name = new String (ch1);
char[] ch2 = new char[80];
String address = new String (ch2);
char[] ch3 = new char[10];
String phone_number = new String (ch3);
char[] ch4 = new char[8];
String date_of_birth = new String (ch4);
double account_balance;

public int get_accountid(){
       return account_id;
}

public String get_address(){
       return address;
}

public String get_phone_number(){
       return phone_number;
}

public String get_date_of_birth(){
       return date_of_birth;
}

public double get_balance(){
       return account_balance;
}

public void set_account_id(int num){
       account_id = num;
}

public void set_address(String add){
       address = add;
}

public void set_phone_number(String phone){
       phone_number = phone;
}

public void set_date_of_birth(String dob){
       date_of_birth = dob;
}

public void set_balance(double bal){
       account_balance = bal;
}
Customer(){ // default constructor
}

// parametrized constructor
Customer(int id, String name, String add, String dob, String num, double bal){
    this.account_id = id;
    this.name = name;
    this.address = add;
    this.date_of_birth = dob;
    this.phone_number = num;
    this.account_balance = bal;
}

}


     public class lab2{

     public static void main(String args[])throws IOException{
     String filename;
     System.out.println("Enter the filename for the input file");
     BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     filename = reader.readLine();
     Customer[] records = readFile(filename);      

     }

      public static Customer[] readFile(String filename)throws IOException{
          Customer[] review = new Customer[30];
          int i=0; 
          Scanner scan = new Scanner (new File (filename));

          while (scan.hasNext()){

               while(i<30){

                      review[i].set_account_id(scan.nextInt());
                  String[] st = scan.nextLine().split("=");
                  review[i].set_address(st[1]);
                  st = scan.nextLine().split("=");
                      review[i].set_phone_number(st[1]);
                      st = scan.nextLine().split("=");
                      review[i].set_date_of_birth(st[1]);
                      //st = scan.nextLine().split("=");
                      review[i].set_balance(scan.nextDouble());
                      scan.nextLine();
                      i=i+1;
             }
        }
            return review;

   }

}

If you look at this line of the stack trace

at lab2.readFile(lab2.java:92)

it indicates that the error occurred on line 92 of your program and the error was InputMismatch when it tried to process the nextInt() call.

Most likely you input something that was not an integer and nextInt() expects an int.

InputMismatchException on line 92
implies your scan.nextInt() is finding in the file something that cannot be parsed as an int.

ps throwing any IOExceptions all the way out of your program is just dumb. Catch it as quickly as possible and execute an e.printStackTrace() to get the maximum info on the error so you can debug/fix it.

To read this stack trace, look at the first line you see. That'll tell you what exception is being thrown, in this case an InputMismatchException. Since this is a Java object produced by Sun, it's documented and you can read up on what Sun has to say about it. The nut of it is this line, short and simple:

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.

So now you know what to look for. Read down the stack trace until you come to a line that you're responsible for. The problem's almost certainly in your code, not in Scanner, and you can't change Scanner from here anyway, so let's stick with the code you wrote. The first line of your code listed is line 92 of lab2.java, so let's look at that.

review[i].set_account_id(scan.nextInt());

It's in a loop (hmmm...) and it's getting a nextInt() - that's certainly a method that would throw this exception. (check the API if you're not sure). What it's telling you is that one of the times through this loop, it found something that it couldn't parse as an int, so it kicked you out with a stack trace.

What it doesn't tell you is where you were in the loop, so you might want to put in a println to tell you the value of i - that'll tell you which iteration gave you the trouble, which should point you to where in the file you're having trouble.

In a more complicated program, you might find that a method fails on some calls and not on others. That's when you read further down the stack trace and see who it was that called this method. In this case, it was called straight out of main(), and it's probably not called anywhere else, but if you have a workhorse method that you call from all over, this can be very useful to figure out why it's dying, because you can see the context in which it's called when it dies.

Hope that helps, best of luck.

If you look at this line of the stack trace

at lab2.readFile(lab2.java:92)

it indicates that the error occurred on line 92 of your program and the error was InputMismatch when it tried to process the nextInt() call.

Most likely you input something that was not an integer and nextInt() expects an int.

The sample input file looks like:

Account Id = 17
  Name = Mohammed Azeem
  Address = Blk 230 #20-116, Yishun Ave 11, Singapore 439772
  DOB = 19-11-1979
  Phone Number = 224-0098
  Account Balance = 1087.03

Okay, so it got to the first line, tried to parse "Account" as an int, and choked, as expected.

There are a couple of ways to do this, but they involve more processing than you're doing now. One could be to read a line and get the part before the equal sign. Use that as a key to tell you what to do next.
As a beginner, you're probably going to be most comfortable doing this as a series of if statements:

String line = scan.nextLine();
// extract the part before the "=" - look up String.split() to learn how to do this
// let's suppose that ends up in a String called key, and the rest of the line ends up in value

if (key.equals("Account Id"))
{  // try to parse value as an integer and use the result as an account id
  // or consider storing the ID as a String, even if it happens to be all digits
  // if you're not calculating with it, that's often a good choice
}
if (key.equals("Name")
{  // store value as a name, or throw it away
}

etc.

It's not a great way to write this, but it'll get you running. Later you can learn about enums and hash maps and other tricks that will make this easier and cleaner code, but for now, just ram it into an if chain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.