【问题标题】:java Scanner splitting stringsjava Scanner分割字符串
【发布时间】:2015-02-27 23:52:27
【问题描述】:

所以我坚持做家庭作业,只需要一点帮助我有 3 个包含多个学生姓名和成绩的文本文件,格式为:

Student Name

Test 1: 93
______________
Student Name

Test 1: 99
_____________

I need to figure out a way to split the line where the grade is and skip the ------- that splits the students and their grades up. I would then need to parse the test grade into a variable so i could do calculations with it. 

     public void openFile() throws IOException {
                //opens text file
                try {
                    FileReader inputFile = new FileReader("C:\\Users\\Chris\\IdeaProjects\\workshop3\\src\\filereader\\file1.txt");
                    //delimiter from scanner object skips characters passed as parameters
                    kb = new Scanner(inputFile).useDelimiter("\n");
                    while(kb.hasNext()){
                        line = kb.next();
                        System.out.println(line);
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }

我现在遇到了这个方法的问题,我正在使用一个数组列表来循环写入学生。但是每次将某些内容写入指定文件时,它都会不断覆盖文件中现有的学生信息。希望有人可以提供帮助。

public void writeFile(Student stud) throws IOException{
        try{
            PrintWriter outputFile = new PrintWriter(new FileOutputStream("C:\\Users\\Chris\\IdeaProjects\\workshop3\\src\\filereader\\file4.txt"));


            outputFile.println("\nStudent: " + stud.getName());
            outputFile.println("Midterm 1: " + stud.getMid1());
            outputFile.println("Midterm 2: " + stud.getMid2());
            outputFile.println("Final: " + stud.getFin());
            outputFile.printf("Average: %.2f"  ,stud.calculateAverage(stud.getMid1(),stud.getMid2(),stud.getFin()));


            outputFile.close();

        }catch(IOException e){

        }
    }

【问题讨论】:

  • 我仍然无法弄清楚为什么这个文件在写入时会不断地重写自己。

标签: java java.util.scanner delimiter


【解决方案1】:

你可以在字符串类中使用 split 函数。

String line= "Test:93";
String[] splits = line.split(":") // which would give {"Test","93"}

【讨论】:

  • 我将如何使用分隔符来解决这个问题,而不是使用拆分方法
  • 你是说每个学生和他的成绩后面都有一个----------行吗?
  • 是的,我需要将学生姓名和成绩输入我可以执行一些平均值的东西,然后将其写入文件。
  • 使用scanner.nextLine insteat 并跳过该特定行
【解决方案2】:

由于您使用的是Scanner,它已经拥有使用\n 作为分隔符的方法nextLine()

学生姓名

测试 1:99

我需要想办法把成绩所在的线和 跳过将学生和他们的成绩分开的-------

参考你的帖子,你可以试试这个:

kb = new Scanner(inputFile);
int grade;
while(kb.hasNextLine()){
    line = kb.nextLine();//read complete line
    if(line.startsWith("----")) {
       continue;//skip it
    }
    if(line.contains(": ")) {
       //student grades
       grade = Integer.parseInt(line.split("\\:\\s")[1]);
    } else {
       //student name
    }
}

在这里,我写的是成绩:Integer.parseInt(line.split("\\:\\s")[1])。由于您提到成绩由: 分隔,line.split("\\:\\s") 将为您提供一个字符串数组,其中包含除定界符之外的值,例如如果输入是Test 1: 99,它会给出["Test 1", "99"]。现在您需要 int 值进行计算,所以我使用了Integer.parseInt(String)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-25
    • 2014-01-25
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 2011-09-12
    • 2020-03-07
    • 1970-01-01
    相关资源
    最近更新 更多