试图创建一个程序,但在删除选项

时间:2015-04-23 19:05:37

标签: java file

如果我在此计划中选择选项3,我希望删除其中一个考试成绩。

如果选择了选项2(视图等级),程序会在此时输出:

1) Bob Jones, Comp, 18
2) Matt Jones, computing, 100
3) Adam Frank-Jones, Drama, 69

正如您在代码中看到的那样,向下增加的数字由" i"表示。我正在寻找的是如果我进入" 1"例如,当选择要删除的行时,它会删除一行(在本例中为#34; 1)Bob Jones,Comp,18")。一行由i +名字+姓氏+课程+标记组成,所有这些都连接在一起。

现在,我已经浏览了互联网代码,帮助我删除一个条目。我试过,如果我等于用户输入,然后删除。我已经尝试使用临时文件,用户按名字搜索(我不确定如何对此进行编码")然后使用BufferedWriter等写回原文。虽然我没有成功完成这项工作。

任何人都可以帮助我吗?感谢

代码:

import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;


public class ExamGrades {

    public static void main(String[] args) throws Exception {
        BufferedReader reader = null;
        FileWriter grades = new FileWriter("grades2.txt",true);
        BufferedWriter bw = new BufferedWriter(grades);
        PrintWriter out = new PrintWriter(bw);
        Scanner scan = new Scanner(System.in);

        int examMark = 0;
        String firstName = "";
        String surName = "";
        String course = "";
        String entry = "";
        String firstCap = "";
        String surCap = "";
        int menu =0;

        System.out.println("Welcome to the 'GradeEnter' program! ");
        System.out.println("Menu: ");
        System.out.println("1) Enter Student Grade(s)");
        System.out.println("2) View Student Grade(s)");
        System.out.println("3) Delete Grade(s)");
        System.out.println("4) Exit");
        menu = scan.nextInt();

        switch (menu) {
        case 1:
            System.out.print("Please enter student first name: ");
            firstName = scan.next(); 

            while(!firstName.matches("[-a-zA-Z]*"))
            {
                System.out.print("Please enter a valid first name: ");
                firstName = scan.next(); 
            }
            firstCap = firstName.substring(0,1).toUpperCase() + firstName.substring(1);


            System.out.print("Please enter student surname: ");
            surName = scan.next();

            while(!surName.matches("[-a-zA-Z]*"))
            {
                System.out.print("Please enter a valid surname: ");
                surName = scan.next();
            }
            surCap = surName.substring(0,1).toUpperCase() + surName.substring(1);

            System.out.print("Please select student course: "); 
            course = scan.next(); 

            System.out.print("Please enter student mark: ");
            while (!scan.hasNextInt())
            {
                System.out.print("Please enter a valid mark: ");
                scan.next();
            }
            examMark = scan.nextInt();

            if (examMark < 40)
            {
                System.out.println("Failed");
            }
            else if (examMark >= 40 && examMark <= 49)
            {
                System.out.println("3rd");
            }
            else if (examMark >= 50 && examMark <= 59)
            {
                System.out.println("2/2");
            }
            else if (examMark >= 60 && examMark <= 69)
            {
                System.out.println("2/1");
            }
            else if (examMark >= 70 && examMark <= 100)
            {
                System.out.println("1st");
            }
            else
            {
                System.out.println("Invalid Mark");
            }

            entry = (firstCap + " " + surCap + ", " + course + ", " + examMark);
            out.println(entry);
            break;

        case 2:
            File file = new File("grades2.txt");
            reader = new BufferedReader(new FileReader(file));

            int i =1;

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(i + ") " + line);    
                i++; 
            }
            break;
        case 3:
            // HERE
            break;
        case 4:
            System.out.println("Thanks for using 'GradeEnter' ");
            System.exit(0);
            break;
        } 

        out.close();
        scan.close();

    }
}

0 个答案:

没有答案