Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Bowler calculate project, two errors

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Bowler calculate project, two errors

    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
    Attached Files Attached Files


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Bowler calculate project, two errors

    There is no such class as NoFileFoundException. Perhaps you mean FileNotFoundException

    Since the code posted will not even compile I doubt you get any exceptions.

    Are you the same person who has posted the exact same assignment at java-forums.org?

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Bowler calculate project, two errors

    Quote Originally Posted by Junky View Post
    Since the code posted will not even compile I doubt you get any exceptions.
    OK, I take that back. I was fooled by the poor indentation and lack of brackets.

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bowler calculate project, two errors

    lol, no i'm just a freshman in college who knows very little about java, and yes i meant FileNotFoundException, my bad. but i can get it to compile...any ideas?

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Bowler calculate project, two errors

    Open a command prompt, navigate to the directory, do a ls command. The most common mistake with text files is that they are called "data.txt.txt".

  6. #6
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bowler calculate project, two errors

    yup solved nothing :/

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Bowler calculate project, two errors

    Likely the file isn't in one of the directories it normally looks (actually not likely but definitely). Where are you placing the file, and how are you running your program (for example, are you using the command-line or and IDE such as eclipse or netbeans).

  8. #8
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Bowler calculate project, two errors

    Try adding a println with 'input' around line 28-29. It should give you the path it's looking for.

  9. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bowler calculate project, two errors

    Thanks for the help but unfortunately i couldn't get it to work, got to class and turns out, there were about 5 kids outs of 40 kids.

Similar Threads

  1. [SOLVED] Using for loop to calculate sine function (factorial issue)
    By Actinistia in forum Loops & Control Statements
    Replies: 2
    Last Post: March 11th, 2011, 03:38 PM
  2. Calculate federal taxes program! Help!
    By ocmjt in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 11th, 2010, 11:25 AM
  3. Check difference between no. of stops, calculate cost
    By JavaStudent23 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 17th, 2009, 03:29 AM
  4. Calculate primes help
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2009, 11:41 PM
  5. Simple java program to calculate wall covering of a room
    By parvez07 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2009, 03:31 PM