Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用arrayList创建菜单并使用文本文件作为输入(bufferedReader)_Java - Fatal编程技术网

Java 使用arrayList创建菜单并使用文本文件作为输入(bufferedReader)

Java 使用arrayList创建菜单并使用文本文件作为输入(bufferedReader),java,Java,正如您在下面看到的,我尝试创建了一个开关盒以供选择 1.名称 2.课程名称 3.年份级别然后是名称 4.然后是课程级别和名称 5.出口 我不知道如何使用开关盒,以便我可以根据菜单对所有东西进行排序。我将使用comparable,我只允许编辑名为compareTo的方法。我的脑子一片空白,不知道从哪里开始。 20192215 昂 布莱恩 M 平衡计分卡 4. 20192200 桑托斯 查理 M BSIT 2. 20192452 蔡 利亚 F BSIS 4. 20190012 怡 约翰 M 平衡计分

正如您在下面看到的,我尝试创建了一个开关盒以供选择 1.名称 2.课程名称 3.年份级别然后是名称 4.然后是课程级别和名称 5.出口 我不知道如何使用开关盒,以便我可以根据菜单对所有东西进行排序。我将使用comparable,我只允许编辑名为compareTo的方法。我的脑子一片空白,不知道从哪里开始。 20192215 昂 布莱恩 M 平衡计分卡 4. 20192200 桑托斯 查理 M BSIT 2. 20192452 蔡 利亚 F BSIS 4. 20190012 怡 约翰 M 平衡计分卡 二,

这些是来自文本文件的输入

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.FileReader;
    import java.io.InputStreamReader;
    import java.util.Arrays;
    import java.util.Scanner;
    import java.util.Vector;

    public class Main {

    /**
     * @param args the command line arguments
     */
        public static void main(String[] args) throws IOException {
            try {
                 BufferedReader br = new BufferedReader(new FileReader("c://student.txt"));
                 char g;
                 int yl, I;
                 String ln, fn, id, cors, con;
                 Student v[] = new Student[4];
                 Scanner sc= new Scanner(System.in);
                 boolean en = true;
                 boolean ent = true;

                for(i = 0; i < 4; i++){
                    id = br.readLine();
                    ln = br.readLine();
                    fn = br.readLine();
                    g = br.readLine().charAt(0);
                    cors = br.readLine();
                    yl = Integer.parseInt(br.readLine());
                
                    v[i] = new Student(ln, fn, id, cors, g, yl);
                 }

            while(en == true){
                System.out.println("--------------------------------------");
                System.out.println("--------------------------------------");
                System.out.println("1. Name");
                System.out.println("2. Course then Name");
                System.out.println("3. Year Level then Name");
                System.out.println("4. Course then Year Level and the Name");
                System.out.println("5. Exit");
                System.out.println("--------------------------------------");
                System.out.println("--------------------------------------");
                System.out.println("Choose Menu: ");
                int choice = sc.nextInt();
                switch(choice){
                    case 1 :
                        Arrays.sort(v);
                        display_array(v);
                    break;
                    case 2 :
                        Arrays.sort(v);
                        display_array(v);
                    break;
                    case 3 :
                        Arrays.sort(v);
                        display_array(v);
                    break;
                    case 4 :
                        Arrays.sort(v);
                        display_array(v);
                    break;
                    case 5 :
                        en = false;
                        System.out.println("\n\n \nTHANK YOU FOR USING THE PROGRAM!!");
                    break;
                }
                if(en != false){
                    System.out.println("Press [Enter key] to continue");
                    try{
                        System.in.read();
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }
            } catch (FileNotFoundException e) {
                System.err.println("File not found");
            } catch (IOException e) {
                System.err.println("Unable to read the file.");
            }
        }
        public static void display_array(Student arr_v[]) throws IOException{
            System.out.println("--------------------------------------");
            for(int i = 0; i < arr_v.length; i++){
                arr_v[i].display();
            }
            System.out.println("--------------------------------------");
        }
    }```
        ```
        public class Student implements Comparable {
            private String lastname, firstname, studentid, course;
            private char gender;
            private int yearlevel;

        public Student(String ln, String fn, String id, String cors, char g, int yl) {
            lastname = ln;
            firstname = fn;
            studentid = id;
            course = cors;
            gender = g;
            yearlevel = yl;
        }

        public int compareTo(Object anotherObject) {
            Student anotherStudent = (Student) anotherObject;
            int compareResult =         
            this.course.compareTo(anotherStudent.lastname); 
            if(compare )
            return 0;  
        }
        public void display() {
            System.out.printf("ID: %-8s  Name: %-20s  Sex: %c  Course: %-8s  Year: %d\n", studentid, (lastname + ", " + firstname), gender, course, yearlevel );
        }

        public void setGender(char gender){
            this.gender = gender;
        }

        public char getGender(){
            return gender;
        }

        public void setLastname(String lastname){
            this.lastname = lastname;
        }

        public String getLastname(){
            return lastname;
        }

        public void setFirstname(String firstname){
            this.firstname = firstname;
        }

        public String getFirstname() {
            return firstname;
        }

        public void setStudentId(String studentid){
            this.studentid = studentid;
        }
    
        public String getStudentId(){
            return studentid;
        }

        public void setCourse(String course){
            this.course = course;
        }

        public String getCourse(){
            return course;
        }

        public void setYearLevel(int yearlevel){
            this.yearlevel = yearlevel;
        }

        public int getYearLevel(){
            return yearlevel;
        }
    }```
导入java.io.BufferedReader;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.FileReader;
导入java.io.InputStreamReader;
导入java.util.array;
导入java.util.Scanner;
导入java.util.Vector;
公共班机{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发IOException{
试一试{
BufferedReader br=新的BufferedReader(新文件阅读器(“c://student.txt”);
charg;
int-yl,I;
字符串ln、fn、id、cors、con;
学生v[]=新生[4];
扫描仪sc=新的扫描仪(System.in);
布尔值en=真;
布尔ent=真;
对于(i=0;i<4;i++){
id=br.readLine();
ln=br.readLine();
fn=br.readLine();
g=br.readLine().charAt(0);
cors=br.readLine();
yl=Integer.parseInt(br.readLine());
v[i]=新生(ln、fn、id、cors、g、yl);
}
while(en==true){
System.out.println(“---------------------------------------------”);
System.out.println(“---------------------------------------------”);
System.out.println(“1.Name”);
System.out.println(“2.课程然后名称”);
System.out.println(“3年级别,然后是名称”);
System.out.println(“4.课程、年级和名称”);
System.out.println(“5.Exit”);
System.out.println(“---------------------------------------------”);
System.out.println(“---------------------------------------------”);
System.out.println(“选择菜单:”);
int choice=sc.nextInt();
开关(选择){
案例1:
数组排序(v);
显示单元阵列(v);
打破
案例2:
数组排序(v);
显示单元阵列(v);
打破
案例3:
数组.排序(v);
显示单元阵列(v);
打破
案例4:
数组排序(v);
显示单元阵列(v);
打破
案例5:
en=假;
System.out.println(“\n\n\n感谢您使用该程序!!”;
打破
}
如果(en!=假){
System.out.println(“按[Enter键]继续”);
试一试{
System.in.read();
}捕获(例外e){
e、 printStackTrace();
}
}
}
}catch(filenotfounde异常){
System.err.println(“未找到文件”);
}捕获(IOE异常){
System.err.println(“无法读取文件”);
}
}
公共静态无效显示数组(Student arr_v[])引发IOException{
System.out.println(“---------------------------------------------”);
对于(int i=0;i4
20192215
Ang
Bryan
m
BSCS
4
20192200
Santos
Charlie
m
BSIT
2
20192452
Chua
Leah
f
BSIS
4
20190012
Yee
John
m
BSCS
2
public final class Student {

    private final String id;
    private final String firstName;
    private final String lastName;
    private final char gender;
    private final String course;
    private final int yearLevel;

    public Student(String id, String firstName, String lastName, char gender, String course, int yearLevel) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = Character.toUpperCase(gender);
        this.course = course;
        this.yearLevel = yearLevel;
    }

    public void display(PrintStream out) {
        out.format("Name       : %s, %s\n", lastName, firstName);
        out.format("Student ID : %s\n", id);
        out.format("Course     : %s\n", course);
        out.format("Gender     : %s\n", gender);
        out.format("Year Level : %d\n", yearLevel);
    }

}

public static void main(String... args) throws FileNotFoundException {
    File file = new File("c://student.txt");
    List<Student> students = readStudents(file);
    display(students);
}

private static List<Student> readStudents(File file) throws FileNotFoundException {
    try (Scanner scan = new Scanner(file)) {
        List<Student> students = new ArrayList<>();

        while (scan.hasNext()) {
            String id = scan.next();
            String lastName = scan.next();
            String firstName = scan.next();
            char gender = scan.next().charAt(0);
            String course = scan.next();
            int yearLevel = scan.nextInt();
            students.add(new Student(id, firstName, lastName, gender, course, yearLevel));
        }

        return students;
    }
}

private static void display(List<Student> students) {
    System.out.println("--------------------------------------");
    System.out.println("--------------------------------------");
    System.out.println("1. Name");
    System.out.println("2. Course then Name");
    System.out.println("3. Year Level then Name");
    System.out.println("4. Course then Year Level and the Name");
    System.out.println("5. Exit");
    System.out.println("--------------------------------------");
    System.out.println("--------------------------------------");

    students.forEach(student -> {
        student.display(System.out);
        System.out.println();
    });
}