学生党小白求各位大佬解救!!!有关于类型转换异常问题
package stuinfosys;
import java.io.*;
import java.util.*;
import java.io.Serializable;
/**
*
*/
public class StuInfoSys implements Serializable
{
private final Map<String,Student>students;
String num,name,sex,school,major;
int age;
public StuInfoSys()
{
this.students=new HashMap();
}
public void addStu()throws IOException,
FileNotFoundException,ClassNotFoundException //如果要实现输入流需要抛出异常-
{ System.out.println("Please put in the information of the new student:");
Scanner in=new Scanner(System.in);
Student stu = new Student();
System.out.println("Please put the num:");
stu.setNum(in.nextLine());
System.out.println("Please put the school:");
stu.setSchool(in.nextLine());
System.out.println("Please put the major:");
stu.setMajor(in.nextLine());
System.out.println("Please put the name:");
stu.setName(in.nextLine());
System.out.println("Please put the sexture:");
stu.setSex(in.nextLine());
System.out.println("Please put the age:");
stu.setAge(in.nextInt());
System.out.println("It's finished!");
students.put(stu.getNum(),stu);
menu2();
}
public void delStu()throws IOException,
FileNotFoundException,ClassNotFoundException //如果要实现输入流需要抛出异常-
{
readData("c:\\StuInfoSys\\data.txt");
Scanner in=new Scanner(System.in);
String actuNum=in.nextLine();
if(this.students.containsKey(actuNum)) //while(ptr.hasNext())
{ //没有break;
this.students.remove(actuNum); //与prtList做对比
}
else
System.out.println("There is no such student in the system");
menu2();
}
/**
*
* @throws IOException
* @throws FileNotFoundException
* @throws java.lang.ClassNotFoundException
*/
public void searchStu()throws IOException,
FileNotFoundException,ClassNotFoundException //如果要实现输入流需要抛出异常-
{
readData("c:\\StuInfoSys\\data.txt");
Scanner in=new Scanner(System.in);
String actuNum=in.nextLine();
if(this.students.containsKey(actuNum))
{
Student stu=(Student)this.students.get(actuNum);//这是返回Value
stu.printStuInfo();
}
else
System.out.println("There is no such student in the system");
menu2();
}
/**
*
* @throws IOException
* @throws FileNotFoundException
* @throws java.lang.ClassNotFoundException
*/
public void editStu()throws IOException,
FileNotFoundException,ClassNotFoundException //如果要实现输入流需要抛出异常-
{
readData("c:\\StuInfoSys\\data.txt");
Scanner in=new Scanner(System.in);
String actuNum=in.nextLine();
if(this.students.containsKey(actuNum))
{
System.out.println("Please input the new student information");
Student newStu=new Student();
newStu.setNum(in.nextLine());
newStu.setName(in.nextLine());
newStu.setSchool(in.nextLine());
newStu.setMajor(in.nextLine());
newStu.setSex(in.nextLine());
newStu.setAge(in.nextInt()); //注意这里不能用NEXTLINE
this.students.replace(actuNum, newStu);
}
else
System.out.println("There is no such student in the system");
menu2();
}
/**
*
* @throws IOException
* @throws FileNotFoundException
* @throws java.lang.ClassNotFoundException
*/
public void prtList()throws IOException,FileNotFoundException,ClassNotFoundException //如果要实现输入流需要抛出异常-
{ //Exception in thread "main"java.lang.ClassCastException:java.util.HashMap cannot be cast to stuinfosys.Student哈希表没有实现Student这个接口。。类型不匹配异常
//内部存储对象的引用时需要调用compareTo方法来排除和去除重复的对象!!
readData("c:\\StuInfoSys\\data.txt");
Set studentNums = this.students.keySet();
Iterator ptr = studentNums.iterator();
while(ptr.hasNext())
{
String actuNum =(String)ptr.next();
Student actuStu =(Student) this.students.get(actuNum);
actuStu.printStuInfo();
}
menu1();
}
public void saveData(String path)
throws IOException,
FileNotFoundException,ClassNotFoundException
{
FileOutputStream fos = new FileOutputStream("c:\\StuInfoSys\\data.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this.students);
oos.flush();
fos.close();
oos.close();
menu1();
}
public void readData(String path)
throws IOException,
FileNotFoundException,ClassNotFoundException
{
FileInputStream fos = new FileInputStream("c:\\StuInfoSys\\data.txt");
ObjectInputStream oos = new ObjectInputStream(fos);
Student loadstu =(Student)oos.readObject();
this.students.put(loadstu.getNum(), loadstu);
fos.close();
oos.close();
menu1();
}
/**
*
* @throws IOException
* @throws FileNotFoundException
* @throws java.lang.ClassNotFoundException
*/
public void menu1()throws IOException,
FileNotFoundException,ClassNotFoundException /
{
System.out.println("Please input the order:");
System.out.println("1.add a student;");
System.out.println("2.delete a student;");
System.out.println("3.print out all the information;");
System.out.println("4.find a student;");
System.out.println("5.edit a student;");
System.out.println("6.exit;");
int order;
Scanner in = new Scanner(System.in);
order=in.nextInt();
switch(order)
{
case 1:
addStu();
break;
case 2:
delStu();
break;
case 3:
prtList();
break;
case 4:
searchStu();
break;
case 5:
editStu();
break;
case 6:
System.out.print("Thanks for your use!");
break;
default:
System.out.print("Mistake!") ; break;
}
}
public void menu2()throws IOException,
FileNotFoundException,ClassNotFoundException
{
System.out.println("Do you want to save the date?:");
System.out.println("1.Yes");
System.out.println("2.No"); int order;
Scanner in = new Scanner(System.in);
order=in.nextInt();
switch(order)
{
case 1:
saveData("c:\\StuInfoSys\\data.txt");
case 2:
menu1();
break;
default:
System.out.print("Mistake!") ; break;
}
}
public static void main(String[] args) throws IOException,
FileNotFoundException,ClassNotFoundException
{
// TODO code application logic here
StuInfoSys sys=new StuInfoSys();
System.out.println("Welcome to the Student Information Management System");
sys.menu1();
}
}
运行时显示:java.util.HashMap cannot be cast to stuinfosys.Student