arrays - Exception : java.lang.ArrayIndexOutOfBoundsException -
i getting error:
exception in thread "main" java.lang.arrayindexoutofboundsexception: 2
@ javaproject.main(javaproject.java:70)
here code:
try { printwriter writer = new printwriter("gamer report data.txt"); writer.println("player: " + gamername); writer.println(); writer.println("-------------------------------"); string[] report = gamerreport.split(gamerreport, ':'); writer.println("game:" + ", score=" + report[1] + ", minutes played=" + report[2] + report[3]); writer.close(); } catch (ioexception e) { system.err.println("file not exist!"); } i believed related for loop, have had no luck changing around.
import java.util.scanner; import java.io.filewriter; import java.io.ioexception; import java.util.arrays; import java.io.printwriter; public class javaproject { private static char[] input; @suppresswarnings("null") public static void main(string[] args) { (int b = 1; b < 100; b++) { // making code loop 100 times int hrs, mins; int[] gamecount; int[] minutesplayed = new int[100]; string gamername, gamerreport; // main data storage arrays string[] gamenames = new string[100]; int[] highscores = new int[100]; scanner scan = new scanner(system.in); // formatting output , input system.out.println("////// game score report generator \\\\\\\\\\\\"); system.out.println(" "); // user enters name , moves next line system.out.println("enter name"); gamername = scan.nextline(); // user given example of input format system.out.println("input gamer information " + "using format --> game : achievement score : minutes played"); system.out.println(" "); system.out.println("game : achievement score : minutes played"); gamerreport = scan.nextline(); string[] splitupreport; // array of string splitupreport = gamerreport.split(":"); // split text on colon int = 0; // copy data split text main data storage arrays gamenames[i] = splitupreport[0]; highscores[i] = integer.parseint(splitupreport[1].trim()); minutesplayed[i] = integer.parseint(splitupreport[2].trim()); // output file try { printwriter writer = new printwriter("gamer report data.txt"); writer.println("player: " + gamername); writer.println(); writer.println("-------------------------------"); string[] report = gamerreport.split(gamerreport, ':'); writer.println("game:" + ", score=" + report[1] + ", minutes played=" + report[2] + report[3]); writer.close(); } catch (ioexception e) { system.err.println("file not exist!"); } } } public static char[] getinput() { return input; } public static void setinput(char[] input) { javaproject.input = input; } }
there 2 problems code:
1)
string[] report = gamerreport.split(gamerreport, ':'); should be
string[] report = gamerreport.split(":"); as did splitupreport (not sure why you're splitting again actually).
2) arrays zero-indexed, print statement should this:
writer.println("game:" + ", score=" +report[0] +", minutes played="+ report[1] + report[2]);
Comments
Post a Comment