有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我试图使一个程序,能够添加新的参与者,并保存在txt文件。该程序还能够从txt文件中读取数据并进行处理。程序可以运行,但当我尝试按状态显示参与者时。我懂java。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

共 (1) 个答案

  1. # 1 楼答案

    首先,您的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);