I need help with an assignment that I have. My assignment asks me to calculate test scores from a text file.
For example, my text file ("input.txt") shows this:
Lois Lane, 100, 98, 95, 90, 93, 88, 92
Marie Calendar, 85, 82, 88, 78, 85, 86, 90
Bob Marley, 70, 75, 72, 78, 80, 82, 76
Tom Brady, 82, 90, 88, 95, 92, 86, 87
Clark Kent, 99, 98, 100, 96, 100, 97, 95
Sandra Dee, 95, 93, 90, 100, 98, 89, 92
I would like to be able to calculate just the scores (by column).
I successfully read the file. However, I am having a hard time figuring out why Integer.ParseInt throws a "NumberFormatException".
This is the code I have so far.
public class testAvg {
public static int main(String[] args) throws IOException {
String token1 = " ";
Scanner inFile = new Scanner(new File("input.txt")).useDelimiter(",\\s*[a-z][A-Z]");
List<String> inputs = new ArrayList<String>();
while (inFile.hasNext()) {
token1 = inFile.next();
inputs.add(token1);
}
inFile.close();
String[] inputsArray = inputs.toArray(new String[1]);
int parsedArray = 0;
for (int i = 0; i < inputsArray.length; i++) {
parsedArray = Integer.parseInt(inputsArray[i]); // This line throws the exception
}
return parsedArray;
}
}
Any help would be greatly appreciated!
input.txt.useDelimiter()call. It actually doesn't work at all, so the entire text is returned bynext(). It works if the regex is changed to"[a-zA-Z\\s]*,\\s*".