java中的例外情况

时间:2011-03-09 14:45:21

标签: java

代码编译正常,但会产生运行时错误,如

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;

   }

}

示例输入文件:

  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 个答案:

答案 0 :(得分:1)

排队

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

scan.hasNextInt()

之前检查scan.nextInt()

答案 1 :(得分:1)

readFile中的while循环无法防止到达文件末尾,因为每次迭代调用scan.nextLine()大约120次。

不确定文件的结构,但是在每次调用nextLine()之后都应检查hasNext(),并考虑重构循环。

从您的示例输入的外观来看,每个条目都在一行上,所以我认为您的readFile方法无论如何都不会起作用。你能修改输入文件格式吗?

答案 2 :(得分:1)

根据javadoc,该异常意味着您的输入不是您认为的那样 http://download.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextInt()

  

InputMismatchException - 如果下一个标记与整数正则表达式不匹配,或者超出范围

您需要检查输入。您还可能希望了解如何使用try / catch来捕获异常并处理它们。