如何解决类案例异常?使用arraylist

hm2xizp9  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(414)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

24天前关门。
改进这个问题
我正在尝试做一个程序,能够添加新的参与者,并将其保存在txt文件中。该程序还能够读取txt文件中的数据并进行处理。程序可以运行,但当我试图按状态显示参与者时。我得到java.lang.classcastexception:java.lang.string不能转换为reading。尝试了几件事,但还是搞不懂

import java.util.* ;
import java.io.* ;

class KRBApp{

  public static int menu(){
    Scanner sc = new Scanner(System.in);

    System.out.println("\n\n\n\t\t\tKRB APPLICATION MAIN MENU");
    System.out.println("\t\t\t~~~~~~~~~");
    System.out.println("\n\t\t\t1. Add new participant");
    System.out.println("\t\t\t2. Display participants by state");
    System.out.println("\t\t\t3. Search participants by name");
    System.out.println("\t\t\t4. Exit");
    System.out.print("\n\t\t\t>>> ");

    int ch = Integer.parseInt(sc.nextLine());

    return ch;
  }//menu

  public static void main(String []args)throws FileNotFoundException, IOException{

     Scanner sc = new Scanner(System.in);
     ArrayList pList = new ArrayList();

     FileReader fr1 = new FileReader("data.txt");
     BufferedReader br1 = new BufferedReader(fr1);
     String read= br1.readLine();

     while(read!= null){ //read file io
           StringTokenizer st = new StringTokenizer(read,";");
           String name =  st.nextToken();
           String phone = st.nextToken();
           String ic = st.nextToken();

           Reading reading = new Reading(name,phone,ic);
           pList.add(read);
           read = br1.readLine();

         }//while

       Reading reading = null;

     File fi1 = new File("data.txt");
     FileWriter fw1 = new FileWriter(fi1,true);
     BufferedWriter bw1 = new BufferedWriter(fw1);
     PrintWriter pw1 = new PrintWriter(bw1);

    int ch = 0;
    do{
      ch = menu();

      if(ch == 1){ // add new participant
         System.out.print("Enter Name : ");
         String name = sc.nextLine();
         System.out.print("Enter Phone Number : " );
         String noTel = sc.nextLine();
         System.out.print("Enter IC Number : ");
         String noIC = sc.nextLine();
         System.out.println(); //new line for each data

         Participant pt = new Participant( name,noTel,noIC);
         pw1.println(pt.toFile());
      }
      else if(ch == 2){ // search by state
        for(int i = 0; i < pList.size() ; i++){
           reading = (Reading)pList.get(i);  // error here 

           System.out.print(reading.getName());
        }
      }
      else if(ch == 3){ //sort by name
        //call method ...
      }
    }while(ch != 4);

    System.out.print("\n\n\n\t\t\tThank You and See You Again! ");
       //close

     pw1.close(); 
  }//public
}//class

读取类从文件io读取数据

class Reading {
    private String name;
    private String phone;
    private String ic;

    public Reading ( String name , String phone , String ic ){
        this.name = name;
        this.phone = phone;
        this.ic = ic;
    }

  public String getName() { return name;}
  public String getPhone() {return phone;}
  public String getIc() { return ic; }

  public String displayAll(){

      return "Name : "+ name + "\n" + "Phone numbers : " +phone + "\n" + "Ic : " +ic + "/n" ;
  }

}//class

此处参与者类用于将数据发送到文件io

class Participant {

private String name;
private String noTel;
private String noIC;

public Participant ( String name , String noTel , String noIC ){
  this.name = name;
  this.noTel = noTel;
  this.noIC = noIC;
}

 public String toFile(){
    return name+";"+ noTel + ";" + noIC ;
  }//toFile

}//class
xn1cxnb4

xn1cxnb41#

首先,你的 pList 是原始类型。这导致它忽略了第二个问题,即向 List . 改变

ArrayList pList = new ArrayList();

List<Reading> pList = new ArrayList<>();

和改变

Reading reading = new Reading(name,phone,ic);
pList.add(read);

Reading reading = new Reading(name,phone,ic);
pList.add(reading);

相关问题