package StudentIO;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class SelectByID {
    //根据id查询数据
    public static  void queryDate() throws IOException {
        //思路:把数据从txt里面读取出来,存入集合,之后遍历集合,根据输入的id与其对比,达到根据id查询出数据的目的
        //1、定义一个从键盘输入的id
        System.out.println("请输入要查询的id");
        Scanner scanner = new Scanner(System.in);
        String id = scanner.next();
        //2、把数据从txt里面取出来放入集合里面
        BufferedReader BR = new BufferedReader(new FileReader("nba.txt"));
        ArrayList<StudentModel> sm = new ArrayList<>();
        String line;
        while((line=BR.readLine())!=null  ) {
            //分割字符串,放入字符数组
            String[] strArray = line.split(",");
            //把数据放入学生对象
            StudentModel s = new StudentModel();
            s.setId(strArray[0]);
            s.setName(strArray[1]);
            s.setAge(strArray[2]);
            s.setHobby(strArray[3]);
            //把学生对象作为元素存储到集合
            sm.add(s);
        }
        //3、遍历集合,把输入的id和studentmodel的id对比,得到对应的student
        int i;
        for (i=0;i<sm.size();i++){
            StudentModel mod = sm.get(i);
            if (id.equals(mod.getId())){
                System.out.println(mod.getId()+","+mod.getAge()+","+mod.getName()+","+mod.getHobby());
            }
        }



    }
}
最近下载更多
29723247  LV8 2024年12月6日
krispeng  LV14 2024年7月11日
HUANGWANG438  LV3 2023年12月6日
poipoiiop  LV8 2023年1月3日
lsglsg9  LV23 2022年9月28日
45645646  LV5 2022年6月22日
2390286321  LV8 2022年6月20日
1719863922  LV11 2022年6月17日
杨小姐好棒棒  LV3 2022年5月23日
Aaakin  LV1 2022年4月30日
最近浏览更多
2636804923  LV6 2月24日
571818771  LV3 2024年12月17日
曾多次  LV1 2024年12月8日
29723247  LV8 2024年12月6日
李清清  LV2 2024年11月6日
wuyanmingsi  LV1 2024年9月7日
krispeng  LV14 2024年7月11日
TY0165  LV20 2024年6月19日
微信网友_6906962132258816  LV7 2024年3月16日
quartz  LV8 2024年3月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友