学生党小白求各位大佬解救!!!有关于类型转换异常问题

qq_36755046 2016-11-19 11:10:21
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
...全文
621 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-11-21
  • 打赏
  • 举报
回复
恭请楼主移驾java论坛。
大熊猫侯佩 2016-11-20
  • 打赏
  • 举报
回复
发错区了吧?

21,458

社区成员

发帖
与我相关
我的任务
社区描述
汇编语言(Assembly Language)是任何一种用于电子计算机、微处理器、微控制器或其他可编程器件的低级语言,亦称为符号语言。
社区管理员
  • 汇编语言
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧