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 5 of 5

Thread: My loop skips index[0] of my array

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default My loop skips index[0] of my array

    Hi, i'm getting really frustrated with my code... as soon as a solve one problem another one comes up. Two problems arose in my code that I've realized are a result of the same problem. I have the user enter the amount of players in a bowling game. Then an array of objects is created for each player. If the user enters 3 for the amount of players, my array has index 0 - 2 as I expected but It asks them for the name of the user starting on index[1] instead of index[0].
     System.out.println("How many players are playing?");
          numberOfPlayers = (input.nextInt() + 1);
          bowlingclass names[] = new bowlingclass[numberOfPlayers];
     
          System.out.println("Enter the name of each player.");
     
          for (int i = 0; i < numberOfPlayers; i++){   
              name = input.nextLine();                
              names[i] = new bowlingclass(name);     //Objects created for each player
          }

    On the next block of code is another for loop where the actual bowling game takes place.

        do {
              frames++;
              for (int i = 0; i < numberOfPlayers; i++){                                       
                  System.out.println("It is "+names[i].name+"'s turn to throw.");
                  System.out.println(names[i].name+" throws his ball...");
                        try { 
                              Thread.currentThread().sleep(3000); 
                         } catch ( Exception e ) { }
                 System.out.println(names[i].name+" hits ") ;
              }
          } while (frames <= 10);
     
        }
    }

    Each iteration of the for loop is a player's turn to bowl. Lets say Joe and Bob are the two names that were entered. The output this code is
    It is 's turn to throw.
     throws his ball...
     hits 10 bowling pins!
     
    It is bob's turn to throw.
    bob throws his ball...
    bob hits 10 bowling pins!
     
    It is joe's turn to throw.
    joe throws his ball...
    joe hits 10 bowling pins!
     
    It is 's turn to throw.
     throws his ball...
     hits 10 bowling pins!

    So it looks like when I assign a name for the three objects, it skips index[0] and starts at index[1].

    This is probably a careless mistake somewhere, but I have tried to fix it for over an hour now and I'm not sure what is wrong. Thank you for any help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My loop skips index[0] of my array

    it skips index[0]
    Where and how is that shown?

    What is the purpose of the folllowing statement:
    numberOfPlayers = (input.nextInt() + 1);
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My loop skips index[0] of my array

    Sorry, I didn't put that in my post. I had two problems arise. My first problem was that in
    System.out.println("How many players are playing?");
          numberOfPlayers = (input.nextInt() + 1);
          bowlingclass names[] = new bowlingclass[numberOfPlayers];
     
          System.out.println("Enter the name of each player.");
     
          for (int i = 0; i < numberOfPlayers; i++){   
              name = input.nextLine();                
              names[i] = new bowlingclass(name);     //Objects created for each player
          }

    When I want 3 player objects to be created, it only lets me enter 2. It looks like to me that it skips index[0] because when the code below is run,
       do {
              frames++;
              for (int i = 0; i < numberOfPlayers; i++){                                       
                  System.out.println("It is "+names[i].name+"'s turn to throw.");
                  System.out.println(names[i].name+" throws his ball...");
                        try { 
                              Thread.currentThread().sleep(3000); 
                         } catch ( Exception e ) { }
                 System.out.println(names[i].name+" hits ") ;
              }
          } while (frames <= 10);
     
        }
    }

    the output is
    It is 's turn to throw.    //there is no name for these three lines
     throws his ball...
     hits 10 bowling pins!
     
    It is bob's turn to throw.
    bob throws his ball...
    bob hits 10 bowling pins!
     
    It is joe's turn to throw.
    joe throws his ball...
    joe hits 10 bowling pins!
     
    It is 's turn to throw.
     throws his ball...
     hits 10 bowling pins!

    It looks like to me that it is skipping index 0 because I want to enter 3 names, but It only allows me to enter 2. Then the output for my second block of code says index[0] is blank (because there is no name for index[0])

    Hopefully that clears it up. I'm sorry if my issue is hard to understand, I'm not very good at explaining things...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My loop skips index[0] of my array

    You are having problems with the Scanner class's methods.
    See: Java Scanner not working, difference between next() and nextLine() | Rommel Rico

    You need to call nextLine() after the call to nextInt()
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Benner (March 22nd, 2013)

  6. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My loop skips index[0] of my array

    Thank you!

Similar Threads

  1. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  2. Array Index Out of bounds?
    By blazeking in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2012, 01:44 AM
  3. [SOLVED] Array Index out of bounds Exception
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2012, 03:03 PM
  4. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 07:11 AM
  5. index of array
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2010, 02:39 AM