exception handling - Java , i keep getting StringIndexOutOfboundsException -
this supposed isolate names , scores , calculate average of scores. output should this:
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
but keeps throwing stringindexoutofboundsexception. why?
this error:
java.lang.stringindexoutofboundsexception: string index out of range: 0 @ java.lang.string.charat(string.java:646) @ calcweightedaverage.extractname(calcweightedaverage.java:49) @ calcweightedaverage.main(calcweightedaverage.java:30)
here code:
import java.util.*; //import java.util scanner import java.io.*;// import java io printwriter /** * class takes input file scores , outputs scores weighted average * * @author * @version 10/30/2015 */ public class calcweightedaverage { /** * method akes input file scores , outputs scores weighted average * * @param string args * @return void */ public static void main(string[] args) throws filenotfoundexception { scanner in = new scanner(system.in); // creating scanner read keyboard system.out.print("please enter name of input file: "); // prompt file name printwriter out = new printwriter("outputfile.txt"); // declares new printwriter outputfile name string filename = in.nextline(); // puts user input filename file infile = new file(filename);//makes new file , puts user input in scanner in1 = new scanner(infile);// declaring scanner 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 next line while (in1.hasnextline()) { string line = in1.nextline(); // new string file string studentname = extractname(line); // extracts student name using method double[] studentscore = new double[5]; // new array store values studentscore = extractvalue(line);// invoke method 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 student name input line. * * @param pline line containing student name, followed number * @return student name */ public static string extractname(string pline) { int = 0; // locate start of first digit while (!(character.isdigit(pline.charat(i)))) { i++; } return pline.substring(0, i).trim(); // extract student name , return } /** * extracts value input line. * * @param pline line containing student name, followed value * @return value associated name */ public static double[] extractvalue(string pline) { int = 0; // locate start of first digit while (!character.isdigit(pline.charat(i))) { i++; } // extract , convert value string scorestring = pline.substring(i).trim(); // extract score , return scanner score = new scanner(scorestring); // new scanner 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 weighted average double[] result = new double[5];// new array 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 } }
the problem in input file has have few spaces otherwise doesnt work
Comments
Post a Comment