利用java访问记事本xinxi.txt,里面为若干行学生信息,要求:筛选出学号为202100946开头并且性别为女的学生信息,符合要求的学生以集合类的形式存储,并按学号升序排序,然后输出。
7条回答 默认 最新
- CSDN专家-sinJack 2021-05-18 15:34关注
//测试类 import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class StudentTest { public static void main(String[] args) throws Exception{ // 设置防止乱码格式(换成自己电脑所在位置) InputStreamReader isr = new InputStreamReader(new FileInputStream( "D:/xinxi.txt"), "utf8"); // 读取文件 BufferedReader br = new BufferedReader(isr); // 创建集合 ArrayList<Student> ar = new ArrayList<Student>(); Student student = null; String line=null; while ((line = br.readLine()) != null) { // 每次循环创建新的学生对象 student = new Student(); // 切割字符串 String[] strAy = line.split(" "); // 设置属性 student.setName(strAy[0]); student.setStuNO(strAy[1]); student.setSex(strAy[2]); student.setAddress(strAy.length==3?null:strAy[3]); if(student.getStuNO().startsWith("202100946")&&student.getSex().equals("女")){ ar.add(student); } } br.close(); // 集合排序使用匿名内部类Collections Collections.sort(ar, new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { // 排序主要条件:按学号升序排序 int num=0; if(Long.parseLong(s1.getStuNO())>Long.parseLong(s2.getStuNO())){ return 1; }else if (Long.parseLong(s1.getStuNO())<Long.parseLong(s2.getStuNO())){ return -1; } return num; } }); for (Student stu: ar) { System.out.println(stu); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记
- ¥15 保护模式-系统加载-段寄存器