gpt4 book ai didi

java - java中的异常

转载 作者:行者123 更新时间:2023-12-02 00:44:54 25 4
gpt4 key购买 nike

代码可以正常编译,但会出现运行时错误,例如

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

最佳答案

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

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

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

关于java - java中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5247411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com