Exceptions in java
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开发者_Go百科_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
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?
In line
review[i].set_account_id(scan.nextInt());
check scan.hasNextInt()
before scan.nextInt()
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.
精彩评论