import java.io.*; import java.util.*; public class Bowler{ File input; static Scanner scan; private String name, game; private String scoresheet = " "; private int[] pins = new int[21]; private int score; private int frame; //constructors private Bowler() { for(int i = 0; i < pins.length; i++) pins[i] = -1; } public Bowler(String n, String g, String filename){ this.name = n; this.game = g; input = new File(filename); try { scan = new Scanner(input); n = scan.nextLine(); g = scan.nextLine(); int i = 0; while(scan.hasNextInt() && i < pins.length) { pins[i] = scan.nextInt(); i++; } scan.close(); }catch(Exception e){e.printStackTrace();} computeScoresheet(); computeScore(); } private void computeScoresheet() { int counter = 0; frame = 1; for(int i = 0; i < pins.length; i++) { if(counter == 1) { counter = 0; continue; } else if(pins[i] == 10) { scoresheet = scoresheet + "X"; counter = 0; frame = frame + 1; continue; } else if(pins[i] < 10) { if(pins[i] + pins[i+1] == 10) { scoresheet = scoresheet + pins[i]+"/"; counter = 1; frame = frame + 1; continue; } else if(pins[i] + pins[i+1] < 10 && pins[i+1] > 0) { int add = pins[i] + pins[i+1]; scoresheet = scoresheet + add + ""; counter = 1; frame = frame + 1; continue; } else if(pins[i] + pins[i+1] == 0) { scoresheet = scoresheet + "-"; counter = 1; frame = frame + 1; } } } } private void computeScore(){ this.score = score; int i, j; score = 0; for (i = 0; i < frame; i++){ if (scoresheet.charAt(i) == 'X'){ score += pins[i]; } for(j = 1; j < 3; j++){ if((i + j) < scoresheet.length()){ score += pins[i + j]; }else if(scoresheet.charAt(i) == '/'){ score += pins[i]; } if((i + 1) < scoresheet.length()){ score += pins[i + 1]; }else score += pins[i]; } } for(i = frame; i < scoresheet.length(); i++){ score += pins[i]; } } //--Instance Methods : Getter/ Accessor Methods -- public String getName(){ return name; } public String getGame(){ return game; } public String getScoresheet(){ return scoresheet; } public int getScore(){ return score; } // -- Instance Methods : Other -- public String toString(){ return(name+" @ "+game+"\n"+ scoresheet+" "+score); } public Bowler(Bowler bowl) { scoresheet = bowl.scoresheet; } @Override public boolean equals(Object o) { if(o instanceof Bowler) { Bowler bowl = new Bowler((Bowler)o); for(int i = 0; i < pins.length; i++) { if(scoresheet == bowl.scoresheet) return true; } }return false; } //---Class Methods : Other --- public static void main(String[] args){ String name = "Steven Amerman"; String game = "Geico owns"; String filename = "data.txt"; Bowler bowl = new Bowler(name, game, filename); System.out.println(bowl.toString()); } }
i get a NoFileFoundException for data.txt, its in the same folder as the Bowler.java file
and then i get an ArrayIndexOutOfBoundsException: 21
any idea how to fix them?
The data.txt file is attached