Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

null trong đọc file của ngôn ngữ java

19:52 13-05-2021 826 lượt xem 1 bình luận 22:10 09-06-2021

Em đang tạo 1 lớp sinhvien trong java và lấy dữ liệu từ bên ngoài váo nhưng lại báo null trong khi file có nội dung và đúng địa chị. Mong mấy ac giúp đỡ

Đây là code của em:

   public void DocFile(String tenFile) {
        try {
            File f = new File(tenFile);
            if (f.exists()) {
                Scanner read = new Scanner(f);
                while(read.hasNext()){
                    SinhVien x;
                    String ma, ht="", qq="";
                    int ngay, thang, nam;
                    boolean gt;
                    double diem;
                    Scanner line = new Scanner(read.nextLine());
                    ma = line.next();
                    while (!line.hasNextBoolean())
                        ht = ht + line.next()+" ";
                        gt = line.nextBoolean();
                    while (!line.hasNextInt())
                        qq = qq + line.next()+" ";
                    ngay = line.nextInt();
                    thang = line.nextInt();
                    nam = line.nextInt();
                    diem = line.nextDouble();
                    x =(new SinhVien(ma,ht,qq, new NgayThang(ngay, thang, nam),gt,diem));
                    a.add(x);
                }
            }else System.out.println("file khong ton tai");
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void output(){
        xuatDS("Danh Sach Sinh Vien Vua Nhap");
        System.out.println("---------------------------");

}

    public static void main(String[] args) {
        MangSinhVien a = new MangSinhVien();
        a.DocFile("D:\\SinhVien.txt");
                //a.input();
        a.output();

    }

}

 

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
Cu Xin Author đã bình luận 21:26 13-05-2021

chuyển text code sang phần code đi bạn.

   public void DocFile(String tenFile) {
        try {
            File f = new File(tenFile);
            if (f.exists()) {
                Scanner read = new Scanner(f);
                while(read.hasNext()){
                    SinhVien x;
                    String ma, ht="", qq="";
                    int ngay, thang, nam;
                    boolean gt;
                    double diem;
                    Scanner line = new Scanner(read.nextLine());
                    ma = line.next();
                    while (!line.hasNextBoolean())
                        ht = ht + line.next()+" ";
                        gt = line.nextBoolean();
                    while (!line.hasNextInt())
                        qq = qq + line.next()+" ";
                    ngay = line.nextInt();
                    thang = line.nextInt();
                    nam = line.nextInt();
                    diem = line.nextDouble();
                    x =(new SinhVien(ma,ht,qq, new NgayThang(ngay, thang, nam),gt,diem));
                    a.add(x);
                }
            }else System.out.println("file khong ton tai");
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void output(){
        xuatDS("Danh Sach Sinh Vien Vua Nhap");
        System.out.println("---------------------------");

}

    public static void main(String[] args) {
        MangSinhVien a = new MangSinhVien();
        a.DocFile("D:\\SinhVien.txt");
                //a.input();
        a.output();

    }

}

 

Câu hỏi mới nhất