这应该隔离名称和分数,然后计算分数的平均值。输出应如下所示:
Student Name Test 1 Test 2 Test 3 Final Average
Susan Smith 76 78 90 100 88.80
Susan Boyd 100 88 79 88 88.60
Alex Chandler 88 99 77 66 79.20
但它不断抛出stringindexoutofboundsexception。为什么呢?
这是错误:
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:646)
at CalcWeightedAverage.extractName(CalcWeightedAverage.java:49)
at CalcWeightedAverage.main(CalcWeightedAverage.java:30)
以下是代码:
import java.util.*; //import java.util for the scanner
import java.io.*;// import java io for printwriter
/**
* class takes input file with scores and outputs scores with weighted average
*
* @author
* @version 10/30/2015
*/
public class CalcWeightedAverage {
/**
* method akes input file with scores and outputs scores with weighted average
*
* @param String args
* @return void
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner in = new Scanner(System.in); // creating scanner to read from the keyboard
System.out.print("Please enter the name of the input file: "); // prompt for file name
PrintWriter out = new PrintWriter("OutputFile.txt"); // declares new printwriter for outputfile name
String filename = in.nextLine(); // puts user input as filename
File inFile = new File(filename);//makes a new file and puts user input in it
Scanner in1 = new Scanner(inFile);// declaring scanner for whats in file
out.printf("%-21s%10s%10s%10s%10s%10s", "StudentName", "Test 1", "Test2", "Test 3", "Final", "Average"); // first line in output
out.println(); // go to next line
while (in1.hasNextLine()) {
String line = in1.nextLine(); // new string from file
String studentName = extractName(line); // extracts the student name using the method
double[] studentscore = new double[5]; // new array to store values
studentscore = extractValue(line);// invoke method to calculate values
out.printf("%-20s%10.0f%10.0f%10.0f%10.0f%10.2f", studentName, studentscore[0], studentscore[1], studentscore[2], studentscore[3], studentscore[4]); // edit scores
out.println();// next line
}
in.close(); // closes scanner
in1.close();//closes scanner
out.close();//closes printwriter
}
/**
* Extracts the student name from an input line.
*
* @param pline a line containing a student name, followed by a number
* @return the student name
*/
public static String extractName(String pline) {
int i = 0; // Locate the start of the first digit
while (!(Character.isDigit(pline.charAt(i)))) {
i++;
}
return pline.substring(0, i).trim(); // Extract the student name and return
}
/**
* Extracts the value from an input line.
*
* @param pline a line containing a student name, followed by a value
* @return the value associated with the name
*/
public static double[] extractValue(String pline) {
int i = 0; // Locate the start of the first digit
while (!Character.isDigit(pline.charAt(i))) {
i++;
}
// Extract and convert the value
String scoreString = pline.substring(i).trim(); // extract score and return
Scanner score = new Scanner(scoreString); // new scanner for score
int testScore1 = score.nextInt(); // value of first score
int testScore2 = score.nextInt();// value of second score
int testScore3 = score.nextInt();// value of third score
int finalscore = score.nextInt();// value of final score
double average = testScore1 * 0.2 + testScore2 * 0.2 + testScore3 * 0.2 + finalscore * 0.4; // calculates the weighted average
double[] result = new double[5];// new array for input
result[0] = testScore1;// value of first score
result[1] = testScore2;// value of second score
result[2] = testScore3;// value of third score
result[3] = finalscore;//value of final score
result[4] = average; // value of average
return result;// return average
}
}
答案 0 :(得分:1)
问题出在输入文件中,它必须有很少的空格,否则它不起作用