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());
            }
        }



    }
}
最近下载更多
HUANGWANG438  LV3 2023年12月6日
poipoiiop  LV8 2023年1月3日
lsglsg9  LV22 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日
龙门客栈  LV9 2021年12月17日
2304360848  LV5 2021年6月29日
最近浏览更多
quartz  LV7 3月13日
暂无贡献等级
fff2003  LV6 1月2日
lshlsh 2023年12月25日
暂无贡献等级
颜菜菜  LV2 2023年12月21日
HUANGWANG438  LV3 2023年12月6日
jiemomo  LV12 2023年11月2日
851690469  LV1 2023年11月2日
wcheng  LV2 2023年10月18日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友