这是一个让我写了近5个小时的程序

如题,这是个让我写了近5个小时的程序,学习 I/O 和 collection。

记下这个程序,来记住现在的我,希望不久再看这个程序时,发现自己的水平有所提高。

程序中还有问题待解决。

Code:
  1. import java.io.*;   
  2. import java.util.*;   
  3.   
  4. public class Test6_Map {   
  5.     public static void main(String[] args) {   
  6.         Scanner sc = new Scanner(System.in);   
  7.         MyMapStu mms = new MyMapStu(new File("C://Users//taotao//Desktop//exp.txt"));   
  8.         Student sno = new Student("91328088""taotao");   
  9.         //sno = sc.next();   
  10.         if(mms.containsKey(sno)) {   
  11.             System.out.println("found");   
  12.         } else {   
  13.             System.out.println("not found");   
  14.         }   
  15.     }   
  16. }   
  17.   
  18. class Student {   //简单定义学生类   
  19.     private String sno;   
  20.     private String sname;   
  21.        
  22.   public Student() {   
  23.   }   
  24.   public Student(String sno, String sname) {   
  25.     this.sno = sno;   
  26.     this.sname = sname;   
  27.   }   
  28.      
  29.   public String getNo() {   
  30.     return sno;   
  31.   }   
  32.   public String getName() {   
  33.     return sname;   
  34.   }   
  35.      
  36. }   
  37.   
  38. class MyMapStu extends Student { //定义此类主要是为了与文件连接,从中把数据读到map中。   
  39.     Map<String, String> map;   
  40.        
  41.     public MyMapStu(File stuInfo) {   
  42.         if(!stuInfo.exists()) {   
  43.             try {   
  44.                 stuInfo.createNewFile();   
  45.                 System.out.println("请建一个拥有30名学生学生信息表!");   
  46.                 BufferedWriter fos = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(stuInfo)));   
  47.                 byte[] bytes = new byte[40];   
  48.                 for(int i=0; i<30; i++) {   
  49.                     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
  50.                     String str = new String();   
  51.                     str = br.readLine();   
  52.                     str += "/n";   
  53.                     //bytes = str.getBytes(str);   
  54.                     fos.write(str, 0, str.length());   
  55.                     fos.write('/n');           
  56.                 }   
  57.                 fos.close();   
  58.           } catch(IOException e) {   
  59.             e.printStackTrace();   
  60.           }   
  61.         }   
  62.            
  63.         try {   
  64.             map = new HashMap<String, String>();   
  65.             BufferedReader fis = new BufferedReader(new InputStreamReader(new FileInputStream(stuInfo)));   
  66.             String stmp = fis.readLine();   
  67.             String[] sArray = stmp.split(" ");    
  68.             Student stu = new Student(sArray[0], sArray[1]);   
  69.             map.put(stu.getNo(), stu.getName());               
  70.         } catch(IOException e) {   
  71.             e.printStackTrace();   
  72.         }   
  73.     }   
  74.        
  75.     public boolean containsKey(Student sno) {   
  76.         if(this.map.containsKey(sno)) {   
  77.             return true;   
  78.         } else {   
  79.             return false;   
  80.         }   
  81.     }   
  82.        
  83. }  

 

containsKey()方法还是有问题,无论怎样都不会找到表中有的数据

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值