weixin_55046612 2021-10-07 17:53 采纳率: 100%
浏览 36
已结题

文件读取出现失败,不知道要怎么改,能帮我修改一下吗

文件读取出现失败,不知道要怎么改,能帮我修改一下吗

img


TeacherInformation:

package dzy;

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

public class TeacherInformation {
    
    private String name;
    private String age;
    private String account;
    private String password;
    ArrayList<Student> array = new ArrayList<>();
    static Scanner scan=new Scanner(System.in);
    
    @Override
    public String toString() {
        return "TeacherInformation [name=" + name + ", age=" + age + ", account=" + account + ", password=" + password
                + "]";
    }
    
    
    
    public TeacherInformation() {
        super();
    }


    public TeacherInformation(String name, String age, String account, String password) {
        super();
        this.name = name;
        this.age = age;
        this.account = account;
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
        void AdmLogin() {
                    
                    BufferedReader br = null;
                    try {
                        String str;
                        br = new BufferedReader(new FileReader("D:\\java-student\\学生管理系统\\学生.txt"));
                        while((str=br.readLine())!=null) {
                            
                            byte[] bytes = str.getBytes();
                            str = new String(bytes,"UTF-8");
                            String num = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            String name = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            String sid = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            String dorm = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            String classes = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            String score = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            String password = str;
                            array.add(new Student(num,name,sid,dorm,classes,score,password));
                            
                    }
                    }catch(Exception e1 ) {
                        System.out.println("读取失败");
                        e1.printStackTrace();
                    }finally {
                        if(br !=null) {
                            try {
                                br.close();
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                        }
                    }
            
            while(true) {
            
        System.out.println("    -------------------------教师管理系统-------------------------");
        System.out.println("\t 0:退出系统并保存\t");
        System.out.println("\t 1:查看个人信息\t");
        System.out.println("\t 2:修改密码\t");
        System.out.println("\t 3:添加学生信息\t");
        System.out.println("\t 4:查询某个学生的成绩.");
        System.out.println("\t 5.查询某个学生的信息");
        System.out.println("\t 6.查看所有学生的信息");
        System.out.println("\t 7:修改学生的成绩.");
        System.out.println("   --------------------------------------------------------------");
        System.out.println("请输入0~7");
        String line=scan.nextLine();
        
        switch(line){
          case"1":
              System.out.println("用户名:"+name+"密码:"+password+"年龄:"+age+"密码:"+account);
              break;
          case"2":
              System.out.println("输入你的新密码:");
              String str = scan.nextLine();
              password = str;
              System.out.println("修改成功,新密码为:"+password);
              break;
          case"3":
                  addStudent();
                break;
          case"4":
              System.out.println("输入要查询学生的学号");
              String tsid = scan.nextLine();
              int i;
              for( i = 0;i<array.size();i++) {
                  if(tsid.equals(array.get(i).getSid())) {
                     break; 
                  }
              }
              if(i>=array.size()) {
                  System.out.println("学生不存在");
              }else {
                  System.out.println("该学生的成绩"+array.get(i).getScore());
              }
              break;
          case"5":
              System.out.println("输入查询学生的学号:");
              String tsid1 = scan.nextLine();
              int i1;
              for( i1 = 0;i1<array.size();i1++) {
                  if(tsid1.equals(array.get(i1).getSid())) {
                     break; 
                  }
              }
              if(i1>=array.size()) {
                  System.out.println("学生不存在");
              }else {
                  System.out.println("该学生的成绩:"+array.get(i1).toString());
              }
              break;
          case"6":
             
              for(int j = 0;j<array.size();j++) {
                  System.out.println("学生:"+array.get(j).getName()+"成绩:"+array.get(j).getScore());
              }
              break;
          case"7":
                  updateStudent();
                  break;
              
          case"0":
              
              try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            
              //BufferedWriter wr = null;
                try {
                    String strs;
                    BufferedWriter wr =new BufferedWriter(new FileWriter("D:\\java-student\\学生管理系统\\学生.txt",false));
                    for(int d=0;d<array.size();d++) {
                        Student stu=array.get(d);
                        str="姓名:"+stu.getName()+",序号:"+stu.getNum()+",学号:"+stu.getSid()+",班级:"+stu.getCclass()+",成绩:"+stu.getScore()+",密码:"+stu.getPassword();
                        wr.newLine();
                        wr.write(str);
                    }
                    wr.close();
                    }catch(IOException e) {
                    e.printStackTrace();
                }
                try{
                BufferedWriter wr = null;
                    br = new BufferedReader(new FileReader("D:\\java-student\\学生管理系统\\学生.txt"));
                while((str=br.readLine())!=null) {
                        System.out.println(str);
                        System.out.println(str.length());
                        byte[] bytes = str.getBytes();
                        str = new String(bytes,"UTF-8");
                        
                    String num = str.substring(0,str.indexOf(","));
                    str = str.substring(str.indexOf(",")+1);
                        
                        String name = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String sid = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String dorm = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String classes = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String score = str.substring(0,str.indexOf(","));
                        str = str.substring(str.indexOf(",")+1);
                        
                        String password = str;
                        array.add(new Student(num,name,sid,dorm,classes,score,password));
                }
                
            }catch(Exception e1 ) {
                System.out.println("读取失败");
                    e1.printStackTrace();
                }
            finally {
                    if(br !=null) {
                        try {
                            br.close();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                    }
                System.out.println("谢谢使用");
                System.exit(0);
            }
          default:
                System.out.println("输入有误,请重新输入!");
            }
        
        }
    }
    
    
     void addStudent() {
        System.out.println("输入用户名:");
        String name =scan.nextLine();
        for(int i = 0 ;i<array.size();i++) {
            if(name.equals(array.get(i).getName())) {
                System.out.println("用户名已存在!");
                return;
            }
        }
        System.out.println("输入序号:");
        String num = scan.nextLine();
        System.out.println("输入学号:");
        String sid = scan.nextLine();
        System.out.println("输入宿舍:");
        String dorm = scan.nextLine();
        System.out.println("输入班级:");
        String classes = scan.nextLine();
        System.out.println("输入成绩:");
        String score = scan.nextLine();
        System.out.println("输入密码:");
        String password = scan.nextLine();
        
        array.add(new Student(num,name,sid,dorm,classes,score,password));
        System.out.println("添加成功!");
        
    }
    

    //修改学生信息
    public  void  updateStudent(){
        

        Scanner sc=new Scanner(System.in);
        if(array.size()==0){
            System.out.println("无信息,请先添加信息再查询!");
            return;
        
        }
                System.out.println("请输入你要修改的学生学号:");
                String sid=sc.nextLine();
                System.out.println("输入序号:");
                String num = scan.nextLine();
                System.out.println("输入姓名:");
                String name = scan.nextLine();
                System.out.println("输入宿舍:");
                String dorm = scan.nextLine();
                System.out.println("输入班级:");
                String classes = scan.nextLine();
                System.out.println("输入成绩:");
                String score = scan.nextLine();
                System.out.println("输入密码:");
                String password = scan.nextLine();
                
                Student s=new Student();
                s.setNum(num);
                s.setName(name);
                s.setSid(sid);
                s.setDorm(dorm);
                s.setCclass(classes);
                s.setScore(score);
                s.setPassword(password);
                
                for(int i=0;i<array.size();i++) {
                    Student student=array.get(i);
                    if(student.getSid().equals(sid)) {
                        array.set(i, s);
                        System.out.println("修改学生信息成功!");
                        return;
                    }
            }
                
                    System.out.println("输入学生信息有误,请重新输入!");
        
            
    }
}

Login:

package dzy;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.*;

public class Login {
    

    LoginFrame loginFrame=new LoginFrame() ;
    
    void login() {
        
        loginFrame.LoginFrame();
    }
}
    
    
    class LoginFrame extends JFrame {
        
        JFrame thisWindow = this;
        
        private ArrayList <Users> ulists = new ArrayList();
        private ArrayList <Student> slists = new ArrayList();
        private ArrayList <TeacherInformation> tlists = new ArrayList();
        
        private static final Integer FRAME_WIDTH=375;
        private static final Integer FRAME_HEIGHT=300;
        
        private JPanel panel;
        private JLabel lblUid;
        private JLabel lblPsw;
        private JRadioButton administrtor;
        private JRadioButton teacher;
        private JRadioButton student;
        private JButton login;
        
        private Administrator administrator;
        private TeacherInformation teac;
        private Student stu;
        
        
        public void LoginFrame() {
            
            this.setTitle("学生管理系统");
            this.setBackground(Color.black);
            setSize(FRAME_WIDTH,FRAME_HEIGHT);
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setResizable(false);
            
            Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();//获得屏幕大小
            int width=screenSize.width;
            int height=screenSize.height;
            setLocation((width-FRAME_WIDTH)/2,(height-FRAME_HEIGHT)/2);
            
            
            panel=new JPanel();
            panel.setBounds(0,0,FRAME_WIDTH,FRAME_HEIGHT);
            panel.setLayout(null);
            this.add(panel);
            
            lblUid=new JLabel("账号");
            lblUid.setBounds(60,38,120,30);
            lblUid.setFont(new Font("宋体",0,16));
            lblUid.setForeground(Color.BLACK);
            panel.add(lblUid);
            
            lblPsw=new JLabel("密码");
            lblPsw.setBounds(60,80,120,30);
            lblPsw.setFont(new Font("宋体",0,16));
            lblPsw.setForeground(Color.BLACK);
            panel.add(lblPsw);
                                             
        
            
            JTextField textUid=new JTextField();//定义账号文本框
            textUid.setBounds(120,40,160,30);
            this.add(textUid);
            
            JPasswordField textPsw=new JPasswordField();//定义密码文本框
            textPsw.setBounds(120,80,160,30);
            this.add(textPsw);
            
            
            login=new JButton("确定");
            login.setBounds(115,150,80,25); 
            panel.add(login);
            
            
            JButton cannel=new JButton("取消");
            cannel.setBounds(205,150,80,25);
            panel.add(cannel); 
            
            
            //设置三个按钮位置
            administrtor=new JRadioButton("管理员");
            teacher=new JRadioButton("教师");
            student=new JRadioButton("学生");
            teacher.setSelected(true);
            ButtonGroup group=new ButtonGroup();
            group.add(administrtor);
            group.add(teacher);
            group.add(student);
            administrtor.setBounds(100,210,75,50);
            teacher.setBounds(185,210,75,50);
            student.setBounds(260,210,75,50);
        
            panel.add(administrtor);
            panel.add(teacher);
            panel.add(student);
            this.add(panel);
            
            login.addActionListener(new ActionListener()  {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    String un = textUid.getText();
                    String pw = textPsw.getText();
                    String str;
                    BufferedReader br = null;
                    try {
                        br = new BufferedReader(
                                new FileReader("D:\\java-student\\学生管理系统\\管理员.txt"));
                        while((str = br.readLine())!=null) {
                            
                            byte[] bytes = str.getBytes();
                            str  = new String(bytes,"UTF-8");
                            
                            String us = str.substring(0,str.indexOf(" "));
                            str = str.substring(str.indexOf(" ")+1);
                            
                            String ps = str.substring(0,str.indexOf(" "));
                            str = str.substring(str.indexOf(" ")+1);
                            String px = str;
                            ulists.add(new Users(us,ps,px));
                    }
                        
                    }catch(Exception e2) {
                        e2.printStackTrace();
                    }finally {
                        if(br !=null) {
                            try {
                                br.close();
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                        }
                    }
                    
                    try {
                        br = new BufferedReader(new FileReader("D:\\java-student\\学生管理系统\\学生.txt"));
                        while((str=br.readLine())!=null) {
                        
                            byte[] bytes = str.getBytes();
                            str = new String(bytes,"UTF-8");
                            
                            String num = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            
                            String name = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            
                            String sid = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            
                            String dorm = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            
                            String classes = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            
                            String score = str.substring(0,str.indexOf(","));
                            str = str.substring(str.indexOf(",")+1);
                            
                            String password = str;
                            slists.add(new Student(num,name,sid,dorm,classes,score,password));
                    }
                    }catch(Exception e1 ) {
                        e1.printStackTrace();
                    }finally {
                        if(br !=null) {
                            try {
                                br.close();
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                        }
                    }
                    try {
                        br = new BufferedReader(new FileReader("D:\\java-student\\学生管理系统\\teacher.txt"));
                        while((str=br.readLine())!=null) {
                            byte[] bytes = str.getBytes();
                            
                            str = new String(bytes,"UTF-8");
                            String name = str.substring(0,str.indexOf(" "));
                            str = str.substring(str.indexOf(" ")+1);
                            
                            String age = str.substring(0,str.indexOf(" "));
                            str = str.substring(str.indexOf(" ")+1);
                            
                            String account = str.substring(0,str.indexOf(" "));
                            str = str.substring(str.indexOf(" ")+1);
                            String password = str;
                            
                            tlists.add(new TeacherInformation(name,age,account,password));
                    }
                        
                    }catch(Exception e1) {
                        e1.printStackTrace();
                    }finally {
                        if(br !=null) {
                            try {
                                br.close();
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                        }
                    }
                    
                    
                    
                    
                    if(administrtor.isSelected()) {
                        for(int i=0;i<ulists.size();i++) {
                            if(
                                    ulists.get(i).getUn().equals(un)&&
                                    ulists.get(i).getPs().equals(pw)) {
                                JOptionPane.showMessageDialog(panel,"登录成功");
                                thisWindow.dispose();
                                administrator = new Administrator(un,pw,"管理");
                                administrator.AdmLogin();
                            
                                return;
                                }
                            }
                        JOptionPane.showMessageDialog(panel,"用户名或密码错误");
                        return;
                    }
                    
                    else if(teacher.isSelected()) {
                        for(int i=0;i<tlists.size();i++) {
                            if(
                                    tlists.get(i).getName().equals(un)&&
                                    tlists.get(i).getPassword().equals(pw)) {
                                
                                JOptionPane.showMessageDialog(panel,"登录成功");
                                thisWindow.dispose();
                                teac = tlists.get(i);
                                teac.AdmLogin();
                                
                                return;
                                }
                            }
                        JOptionPane.showMessageDialog(panel,"用户名或密码错误");
                        return;
                    }        
                        
                    else if(student.isSelected()) {
                        for(int i=0;i<slists.size();i++) {
                            System.out.println(slists.get(i));
                            if(    
                                    slists.get(i).getName().equals(un)&&
                                    slists.get(i).getPassword().equals(pw)) {
                                
                                JOptionPane.showMessageDialog(panel,"登录成功");
                                thisWindow.dispose();
                                stu = slists.get(i);
                                stu.AdmLogin();
                                return;
                                }
                            }
                        JOptionPane.showMessageDialog(panel,"用户名或密码错误");
                        return;
                    }
                    
                }
            });
            
            
            //取消按钮功能实现
            cannel.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                
                    thisWindow.dispose();        
                }
            });

            setVisible(true);
            
        }

    }

  • 写回答

1条回答 默认 最新

  • CSDN专家-sinJack 2021-10-07 18:06
    关注

    indexOf()返回-1.没有找到逗号对应的索引。

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月15日
  • 已采纳回答 10月7日
  • 创建了问题 10月7日

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c