【问题标题】:Separating name from numbers in text file将文本文件中的名称与数字分开
【发布时间】:2018-10-18 15:51:21
【问题描述】:

我想要做的是读取一个看起来像这样的文本文件:

Tom 96 95 94 93 54

并将名称和数字分开。然后我将打印学生的姓名总分(所有数字的总和)和平均值。我有一个名为 Student 的单独课程,其中包含课程:

private String name;
   private int numOfQuizzesTaken;
   private int totalScore;
   public Student(String name){
       this.name = name;
   }
   public String getName() {
       return name;
   }
   public int getTotalScore() {
       return totalScore;
   }
   public void addQuiz(int score) {
       totalScore = totalScore + score;
       numOfQuizzesTaken++;
   }
   public double getAverageScore(){
       return totalScore/(double)numOfQuizzesTaken;
   }
}

我知道我需要使用一个数组,但我不知道如何将名称与数字分开并存储这些要使用的数字。 我写了一个没有txt文件的类似代码,它看起来像这样:

Scanner nameInput = new Scanner(System.in);
    System.out.print("What is your name? ");
    String name = nameInput.next();

    Student student = new Student(name);


    student.addQuiz(96);
    student.addQuiz(94);
    student.addQuiz(93);
    student.addQuiz(92);
       System.out.println("Students Name: " + student.getName());
       System.out.println("Total Quiz Scores: " + student.getTotalScore());
       System.out.println("Average Quiz Score: " + student.getAverageScore());

希望这能让我对我正在尝试做的事情有所了解,并希望我能得到任何帮助。

【问题讨论】:

  • 如果你的文本文件保持你提到的格式,你只需要逐行阅读并用“”分割行,那么你有一个包含名称和分数的字符串数组。
  • 您必须将文件读取为字符串,用“”分隔并使用Integer.toInt("95')解析数字
  • 格式应该相同,但数字的数量和名称的长度可能会有所不同。但从你所说的来看,这可能不会影响@aussie

标签: java arrays class arraylist text


【解决方案1】:

您需要将文件读入字符串,然后用“”分割。从那里你将有第一个索引是名称,其他索引是分数

File score = new File("quiz.txt");
Scanner scn = new Scanner(new FileInputStream(score));
String buffer = "";
while (scn.hasNext()) {
      buffer += scn.nextLine() + "\n";
}
String[] content = buffer.trim().split(" ");
String name  = content[0] ;
Student student = new Student(name);
for (int a = 1 ; a < content.length; a++) {
    int scoreval = Integer.parseInt(content[a]);
    student.addQuiz(scoreval);
}

【讨论】:

  • 所以我对第一行分数有疑问。 txt 文件称为 TextFile。如何更改分数以使用该 txt 文件?我真的很喜欢这个,这就是我需要的
  • score是txt文件File score = new File("quiz.txt");quiz.txt是文本文件的相对位置,
  • 我已经解决了这个问题,但仍然找不到文件。我的文本文件是否在错误的位置?
  • 你肯定把它放在了错误的位置。首先检查PC上文件的绝对路径,然后使用File score = new File("absolute/path/to/quiz.txt");表达式中的绝对路径,如果它有效,那么你就可以开始处理相对路径了。在我的示例中,我假设文本文件位于您的项目目录中
【解决方案2】:

如果文本文件将具有以下格式,您可以读取一整行,然后将字符串按" " 字符分割。数组中的第一个元素将是学生的姓名,其余元素将是分数。这是一个例子:

String s = fileReader.nextLine();

String[] split = s.split(" ");

Student student = new Student(split[0]);

Arrays.stream(split).skip(1) // skip the first element as it's a name
    .forEach(score -> student.addQuiz(Integer.parseInt(score)));

【讨论】:

  • 我不断收到错误:.forEach(score -> student.addQuiz(Integer.parseInt(score)));合规性错误
  • 你使用的JDK是什么版本的?
  • 8 我相信我正在使用 eclipse。
【解决方案3】:

您可以使用“”解析输入,将其存储在字符串或对象数组中,如下所示,这个

“汤姆 96 95 94 93 54”

代表你从用户那里读取的输入,它应该是一个变量,我假设你正在一次读取学生信息(姓名等...)

    String[] studentInfoArray = "Tom 96 95 94 93 54".split(" ");
    String name = studentInfoArray[0];
    Integer quiz1 = Integer.parseInt(studentInfoArray[1]);

【讨论】:

  • 这只是一个代码示例,您可以使用 for 循环读取其余的数组元素。
【解决方案4】:

我认为您应该使用正则表达式将数字与名称分开。一旦你提取了包含数字的字符串,你可以用空格分割它(这将返回一个数字数组)并创建一个哈希映射来存储数据(只有当你有唯一的名称时),键是名称和数组测验分数是价值。希望这可以帮助。 :)

您可以将数字解析为整数或浮点数,然后再将其存储到哈希映射中。如果名称只有字母,那么这个正则表达式应该这样做:([A-Z,a-z])*\x20*(\d.*)

  • 组 1 将匹配名称。
  • 第 2 组将为您提供包含数字的字符串,让我们将其存储在 String quizScoreString

    String[] stringScores= quizScoreString.split("\s+");

【讨论】:

    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2011-08-18
    • 2013-09-21
    相关资源
    最近更新 更多