# GuessingGame problem

• March 22nd, 2011, 03:51 PM
Stockholm Syndrome
GuessingGame problem
Hello, just wondering if anyone can help me with this problem. I had write a program which creates 100 random integers, each in the range 1 to 1000, inclusive. The user guesses a number and is told whether or not it is among the random numbers & this continues until the user guess correctly or enters 0 to signal that he/she gives up. The program had to include a static function called member which returns a boolean value indicating whether or not an integer x occurs in an integer array w where x and w are parameters.

Here is my code:
Code Java:

```class GuessGame { static boolean member(int x, int[] w)//returns true if x occurs in array w { int i=0; while(i<w.length && i!=x) i++; if(i<w.length) //if found return true; else return false; } public static void main(String[] args) { int[] nums = new int[1000]; for(int i=0; i<nums.length; i++)// fill array with random numbers in the range 1..1000 { nums[i] = (int)(1+(Math.random()*1000)); } System.out.println("Take a guess.."); int guess = Console.readInt(); while(guess!=0) { if(member(guess, nums)) { System.out.println("This number is among the random numbers."); break; } System.out.println("This number is not among the random numbers, try again."); guess = Console.readInt(); } }//end main }//end class```

When i run this, it keeps saying the number is among the random numbers, even if it's not. Can anyone help me with this? Thank you
• March 22nd, 2011, 04:35 PM
newbie
Re: GuessingGame problem
Code java:

``` if(i<w.length) //if found return true;```

Think about that for a minute, what you're essentially saying is...
If 1(and upwards) is less than 1000, return true.

How does 1 being less than 1000 equate to x being among w?
• March 22nd, 2011, 04:41 PM
JavaPF
Re: GuessingGame problem
I also suggest reading - Debugging with System.out.println
Then you can see exactly what is going on..
• March 22nd, 2011, 05:50 PM
Stockholm Syndrome
Re: GuessingGame problem
Sorry yes i understand what you mean. I was looking at another example I had done and thats what was embedded in the if statement. What could i use to determine if x is found? Would it be correct to use: if(w[i]==x) return true; ??
• March 22nd, 2011, 06:04 PM
newbie
Re: GuessingGame problem
Yeah, that would be what you're after.
Also consider replacing the while loop with a for loop, it's more convenient than storing a counter variable yourself.
Of course it will function exactly the same as your while, but it's ultimately down to your preference I suppose.

Regarding your program specification, int[] nums = new int[1000];
is a waste of memory, as you only need to store 100 not 1000 Integers.

(int)(1+(Math.random()*1000));
Deals with doubles, and you're obviously casting it into an int.

I'd personally recommend using nextInt(int n) method from the Random class for generating random numbers, but again, its up to you :).
Have a go implementing what you suggested, and come back if you get stuck again.

Random (Java 2 Platform SE v1.4.2)
• March 22nd, 2011, 06:20 PM
Stockholm Syndrome
Re: GuessingGame problem
Quote:

Originally Posted by newbie
Yeah, that would be what you're after.
Also consider replacing the while loop with a for loop, it's more convenient than storing a counter variable yourself.
Of course it will function exactly the same as your while, but it's ultimately down to your preference I suppose.

Regarding your program specification, int[] nums = new int[1000];
is a waste of memory, as you only need to store 100 not 1000 Integers.

(int)(1+(Math.random()*1000));
Deals with doubles, and you're obviously casting it into an int.

I'd personally recommend using nextInt(int n) method from the Random class for generating random numbers, but again, its up to you :).
Have a go implementing what you suggested, and come back if you get stuck again.

Random (Java 2 Platform SE v1.4.2)

Thanks very much for the help I appreciate it :) Regarding the loops, the only reason i use a while loop is because my lecturer showed us that way & said its the simplest way :D Personally, I don't really see much of a difference in simplicity. The program is now running ok, but if i enter a number that isn't found in the array, it throws this exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at GuessGame.member(GuessGame.java:10)
at GuessGame.main(GuessGame.java:26)

any ideas how to fix this?
• March 22nd, 2011, 06:28 PM
newbie
Re: GuessingGame problem
I'm going to assume you haven't changed much in you're while loop and say the following:
An array of size 100, has indices ranging from 0 99 inclusive.

Because you're incrementing i, (i++;) before the if statement, your first pass is going to check index 1 and your last pass of the loop is going to check 100 instead of 99.
To resolve this, move the increment to the bottom of the loop.
• March 22nd, 2011, 06:39 PM
Stockholm Syndrome
Re: GuessingGame problem
Sorry I dont fully understand. i++ is the only thing present in the loop body, i'm not sure where you mean to put it
Code Java:

```static boolean member(int x, int[] w) { int i=0; while(i<w.length && w[i]!=x) { i++; } if(w[i]==x) return true; else return false; }```
• March 22nd, 2011, 07:02 PM
newbie
Re: GuessingGame problem
Oh i see.
Well that loop currently does pretty much nothing.
You need to place the if and else statements inside the while loop.

What's giving you the error is, you're incrementing i from 0 to 100 inside the while loop, and after you exit, you try and access index 100, which If you have set your array to size 100, doesnt exist as Array indices start from 0 inclusive.
• March 22nd, 2011, 07:07 PM
Stockholm Syndrome
Re: GuessingGame problem
All was needed was to change while(i<w.length.....) to while(i<w.length-1....) . Works perfectly now. Thanks for your help once again :)
• March 22nd, 2011, 07:11 PM
newbie
Re: GuessingGame problem
But it doesn't work perfectly, it will only return true if x is equal to index 99 of W.
Follow what I wrote and you'll get what you want.