0

The code compiles fine but gives run time errors like

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)

I'm not really sure what the issues are a I have thrown the exceptions in the code which is as follows.

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;

   }

}

SAMPLE INPUT FILE:

  Account Id = 70
  Name = Tan Beng How
  Address = Blk 111 #05-06, Nanyang Avenue, Singapore 639798
  DOB = 12-07-1979
  Phone Number = 799-8765
  Account Balance = 2324.23

  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
3
  • 4
    Post the errors you are receiving. Commented Mar 9, 2011 at 14:46
  • Where in your code is the exception occurring? Commented Mar 9, 2011 at 14:47
  • 1
    any details on the exception? Which line does it complain about? What exception is it? Commented Mar 9, 2011 at 14:48

3 Answers 3

1

Your while loop in readFile won't protect against reaching the end of the file, as you're calling scan.nextLine() approx 120 times per iteration.

Not sure about the structure of your file, but you should check hasNext() after each call to nextLine(), and consider restructuring your loops.

From the looks of your sample inputs, each entry is on one line, so I don't think your readFile method will work anyway. Are you able to modify the input file format?

Sign up to request clarification or add additional context in comments.

2 Comments

ahh the input file has each column in separate line. stack overflow doesn't let me edit it properly :/. so there's the account id then name in the next line and so on. there is a one line space b/w different accounts.
Aha, I see now. Your calls to scan.nextInt() and scan.nextDouble will be the problem causing the exception you see now. The reader will be attempting to read at the start of the line, which will be "Account Id = ", and not an Int. You'll need to split the line around the = as you do in the other lines and parse the value with Integer.parseInt(st[1]) and Double.parseDouble(st[1]).
1

In line

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

check scan.hasNextInt() before scan.nextInt()

Comments

1

As per the javadoc, that exception means your input isn't what you think it is http://download.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextInt()

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range

You need to check your input. You also may want to look at how to use try/catch to catch exceptions and deal with them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.