- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这应该隔离姓名和分数,然后计算分数的平均值。输出应如下所示:
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
}
}
最佳答案
问题出在输入文件中,它必须有很少的空格,否则它不起作用
关于Java,我不断收到 StringIndexOutOfboundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446947/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!