Reputation: 1652
I get the error on the line " temp.set_account_id(Integer.parseInt(st[1].trim()));". here is the code, i don't know why it is giving me an exception. help will be appreciated.
public static int readFile(String filename, Customer[] review)throws IOException{
int count=0;
Scanner scan = new Scanner (new File (filename));
/*Reading the first record separatly*/
Customer first = new Customer();
String[] a = scan.nextLine().split("=");
first.set_account_id(Integer.parseInt(a[1].trim()));
a = scan.nextLine().split("=");
first.set_name(a[1].toUpperCase().trim());
a = scan.nextLine().split("=");
first.set_address(a[1].trim());
a = scan.nextLine().split("=");
first.set_phone_number(a[1].trim());
a = scan.nextLine().split("=");
first.set_date_of_birth(a[1].trim());
a = scan.nextLine().split("=");
first.set_balance(Double.parseDouble(a[1].trim()));
review[0]= first;
count = count+1;
while (scan.hasNext()&& count>0){
Customer temp = new Customer();
String[] st = scan.nextLine().split("=");
for(int i=1;i<count;i++){
if(Integer.parseInt(st[1].trim())== review[i].get_accountid()){ // checking for duplicate records
System.out.println("This account id is already in use so the record won't be read");
for (int k=0; k<6; k++)
scan.nextLine();
}
else
break;
}
temp.set_account_id(Integer.parseInt(st[1].trim()));
st = scan.nextLine().split("=");
temp.set_name(st[1].toUpperCase().trim());
st = scan.nextLine().split("=");
temp.set_address(st[1].trim());
st = scan.nextLine().split("=");
temp.set_phone_number(st[1].trim());
st = scan.nextLine().split("=");
temp.set_date_of_birth(st[1].trim());
st = scan.nextLine().split("=");
temp.set_balance(Double.parseDouble(st[1].trim()));
if (scan.hasNextLine()){
scan.nextLine();
}
int j;
for(j=0;j<count;j++){
if (temp.get_name().compareTo(review[j].get_name())<0){ // Putting records in ascending order
break;
}
}
count=count+1;
for (int k=count;k>j;k--){
review[k]=review[k-1];
}
review[j]= temp;
if (count>=30){
System.out.println("The number of records read has exceeded the limit and it will stop reading now");
break;
}
}
System.out.println("The number of records read= " + count);
//System.out.println(count);
return count;
}
Upvotes: 0
Views: 518
Reputation: 1652
aah i forgot to move forward after reading in the first record. i just did a couple of nextline() so that the input string won't be empty.
Upvotes: 0
Reputation: 88727
If scan.nextLine()
would return a string with no =
you'd get an array of length 1. You access the second element [1]
and if there is none, you get that exception.
Upvotes: 0
Reputation: 16844
Most likely the scanned line in String[] a = scan.nextLine().split("=");
does not contain a '=' character. Take a debugger and check the value returned by scan.nextLine()
and a
.
Upvotes: 1
Reputation: 554
try watching the variable st by having a break point at line. set_account_id(Integer.parseInt(st[1].trim()))
It would be an empty string
Upvotes: 3