output giving null, methods not reading properly
Whenever I try to run the program, its giving me null for my scoresheet and 0 for my score. And my toString() is returing null but my System.out.println(l.getName()); and System.out.println(l.getGame()); are returning the proper strings. But i need to have my score and scoresheet to read properly. Thanks for the help.
Code:
import java.lang.*;
import java.util.*;
import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
public class Bowler{
private int [] pins;
private String name, game, scoresheet;
private int score;
private Bowler(){
pins = new int[16];
}
public Bowler(String name, String game, String f){
this.name = name;
this.game = game;
String filename = f;
try{
Scanner s = new Scanner(new FileReader("bowl.txt"));
int i = 0;
while(s.hasNextLine()){
pins[i] = s.nextInt();
i++;
System.out.println(pins[i]);
}
s.close();
}catch(Exception e){e.printStackTrace();}
}
private void computeScoresheet(){
int i = 0;
for(int frame = 1; frame < 10; frame++) // frames 1-9
{
if(pins[i] == 10)
{
scoresheet += "X";
i++; // observed one element
}
else if(pins[i] + pins[i+1] == 10)
{
scoresheet += pins[i] + "/";
i+= 2; //observed two elements
}
else if(pins[i] != 0)
{
scoresheet += pins[i];
i++;
}
else{
scoresheet += "-";
}
}
}
private void computeScore(){
score = 0;
int i, j;
int lastFrameIndex = 10;
for(i = 0 ; i < lastFrameIndex; 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) < 10){
score += pins[i];
if(i+1 < scoresheet.length())
score += pins[i+1];
}}
for(i = lastFrameIndex; i < scoresheet.length(); i++)
score += pins[i];
}
}
public String getName(){
try{
Scanner s = new Scanner(new FileReader("bowl.txt"));
name = s.nextLine();
s.close();
}catch(Exception e){e.printStackTrace();}
return name ;
}
public String getGame(){
try{
Scanner s = new Scanner(new FileReader("bowl.txt"));
s.nextLine();
game = s.nextLine();
s.close();
}catch(Exception e){e.printStackTrace();}
return game ;
}
public String getScoresheet(){
this.computeScoresheet();
return this.scoresheet;
}
public int getScore(){
this.computeScore();
return this.score;
}
public String toString(){
return name+" "+ game;
}
public boolean equals(Object o){
return true;
}
public static void main(String[] args) {
Bowler l = new Bowler();
System.out.println(l.toString());
System.out.println(l.getName());
System.out.println(l.getGame());
}
}