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

Thread: Guessing Game Program Problem.. ArrayIndexOutOfBoundsException while printing?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Guessing Game Program Problem.. ArrayIndexOutOfBoundsException while printing?

    I am having a strange problem with my Guessing Game program. The program takes an argument integer between 1 and 100 as a secret number, and the program will guess random numbers until it finds your number. Each incorrect guess is recorded in a vector and the guesses are printed after the secret number is found. It seems to work perfectly fine except that when printing out the guesses, I get an ArrayIndexOutOfBounds exception. Can anyone see the problem?? -Thanks

    import java.util.Vector;
     
    public class GuessingGame{
    	int target;
    	int counter = 0;
    	Vector<Integer> guesses;
     
    	public static void main(String []args){
    		int secretNumber = Integer.parseInt(args[0]);
    		GuessingGame g = new GuessingGame(secretNumber);
    		g.startGuessing();
    		g.printGuesses();
    	}
     
    	public GuessingGame(int i){
    		target = i;
    		guesses = new Vector<Integer>(100, 25);
    	}
     
    	public void startGuessing(){
    		while(1 == 1){
    			int guess = 1 + (int) (Math.random() * (100 - 1) + 1);
    			if(guess == target){
    				System.out.println("Target Found!  " + guess);
    				break;
    			}else{
    				guesses.add(guess);
    			}
    			counter++;
    		}
    	}
     
    	public void printGuesses(){
    		for(int i = 0; i < guesses.capacity() - 1; i++){
    			System.out.println("Guesses: " + guesses.elementAt(i));
    		}
    	}
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Guessing Game Program Problem.. ArrayIndexOutOfBoundsException while printing?

    I suspect that the capacity of an instance of Vector is not what you think it is. What it is is explained at the start of the Vector API docs.

    Almost certainly you should be dealing with the collection's size not its capacity. There is a size() method for obtaining this. Also Vector has a no argument constructor and you should use that unless you have some reason not to. (And that reason will be based on an understanding of what capacity means...)

    ---

    Once you have the program doing what you want with Vector consider rewriting it to use one the more modern List implementations like ArrayList. This would be the most straight forward approach again, unless your program has some compelling reason to use Vector. The only such reason that I've some across involves Swing (a graphical user interface) and your program doesn't use Swing.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Exiled (December 11th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game Program Problem.. ArrayIndexOutOfBoundsException while printing?

    Thanks! That was exactly the problem, I thought there was a size() method for Vectors but I couldn't remember, and the only reason I am using vector for this is because the assignment calls you using a vector Thanks for your help pbrockway2

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Guessing Game Program Problem.. ArrayIndexOutOfBoundsException while printing?

    You're welcome.

Similar Threads

  1. JAVA Guessing Game Problem
    By adnan.alvee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2012, 12:02 AM
  2. Guessing Game
    By Spark2.0 in forum Object Oriented Programming
    Replies: 4
    Last Post: June 6th, 2012, 12:14 PM
  3. Guessing game help
    By np657 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 12th, 2012, 07:39 AM
  4. guessing game
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 02:30 PM
  5. Java Newbie..need help in guessing game program
    By confupavan in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 16th, 2011, 09:22 AM

Tags for this Thread