【发布时间】:2018-08-18 17:53:29
【问题描述】:
我遇到了这个特定程序的问题。长话短说,这个程序应该做的是从文本文件中获取输入,在本例中是投球手的姓名和得分,并从中读取数据。我遇到的问题是尝试从文件中提取分数并将它们存储在单独的变量中。 `
import java.util.*;
import java.io.*;
public class lab5{
public static void main (String args[]) throws IOException {
String bowler;
String name;
int score1 = 0, score2 = 0, score3 = 0;
int game = 0;
int average = 0;
//Scanner scan = new Scanner (new FileReader("bowl1.txt."));
Scanner scan = new Scanner (new File ("bowl1.txt"));
while (scan.hasNext()){
bowler = scan.nextLine();
System.out.println("\n" + bowler);
String charCheck = "";
String indent = "";
int count = bowler.length() - 1;
while (count <= bowler.length()){
indent = bowler.lastIndexOf(" ");
charCheck = bowler.substring(0,indent);
System.out.println(charCheck);
count++;
score1 = Integer.parseInt(charCheck);
score2 = Integer.parseInt(charCheck);
score3 = Integer.parseInt(charCheck);
}//end while
System.out.println(score1 + score2 + score3);
}//end fileScan while
}//end main
}//end class
抱歉,我的代码有点草率,但我仍在学习一些 java 的基础知识。基本上,这里的想法是我需要使用 Substring 和 lastIndexOf(" ") 方法从文件中读取分数,然后使用 Integer.parseInt() 将它们转换为整数。
(文件名为“bowl1.txt”,这是上面的数据)
阿特基森,达伦 188 218 177
巴恩斯,克里斯 202 194 195
安东尼·多兰 203 193 225
伊斯顿,查尔斯 255 213 190
任何关于我所缺少的帮助或提示将不胜感激。
【问题讨论】:
-
此链接可能对您有用。 stackoverflow.com/questions/1903252/…