# [Method] Help ?

• November 28th, 2010, 08:26 PM
hmafia
[Method] Help ?
Hey first time poster, first year of computer programming so I thought to myself, why not sign up for a forum ! So here I am ahah.

Anyways I'm a bit stumped on my assignment, and would like a bit of clarification, I'll show you what I've got so far.

Basically it's the old Jelly Bean game, ask users how many will be playing, and then they enter their names, their guesses and so on.

Here's my code so far.

Code Java:

```import java.util.Scanner;   public class M_H_Project2JellyBeans {   public static void main(String[] args) { Scanner input = new Scanner(System.in);     //introduce the program to the user System.out.print ( "\t Welcome to Moe's Jelly Bean Guessing Game! " + "\n\t ******************************************" + "\n Can you guess correctly how many Jelly Beans are in the jar ? " + " \n -The number of Jelly Beans in the jar ranges from 1001 to 1999- " );   //ask how many people will be making a guess System.out.print( "\n\n How many people will be attempting to guess the right number today ? " ); int arraySize = input.nextInt();   //create a String array for the names and an int array for the guess String[] nameArray = new String[arraySize]; int[] guessArray = new int[arraySize]; int xNumber; //create random jelly bean number int jellyBeans =(int)(Math.random()*(1999-1001+1) + 1001);       //set up a for loop to enter the data for(int i = 0; i < nameArray.length; i++) {   input.nextLine(); //clear the buffer   //ask for name of person   System.out.print("Enter name of person #" + (i+1)+ ":"); nameArray[i] = input.nextLine();   //do-while loop do { //Ask the user to enter their guess. System.out.print(nameArray[i] + ", Enter your guess between 1000 and 2000: "); guessArray[i] = input.nextInt();   //Let user know they've entered an invalid number if number they've entered //does not range from 1001 to 1999 ! if ( guessArray[i] <= 1000 || guessArray[i] >= 2000 ) { System.out.println("Don't be silly ! Your guess has to be between 1001 to 1999...Please try again."); }   } while(guessArray[i] <= 1000 || guessArray[i] >=2000);     { System.out.println("\t\t\t\t[*" + nameArray[i] + " has entered '" + guessArray[i] + "' as a guess*]" ); }     if ( guessArray[i] == jellyBeans) { System.out.println("TheThe winner is" + nameArray + "with a guess of " + jellyBeans + ", which is exactly the same as the number of jelly beans in the jar."); }   } System.out.println("\nThere were " + jellyBeans + " Jelly beans in the jar");   int lowestDifference = findLowestDifferencehelper.findLowestDifference(jellyBeans, guessArray[0]);     System.out.println( "Your guess was " + lowestDifference + " jelly beans away from the actual number.");               }   }```

And here's the method i've created to findLowestDifference() of the users guess and the actual randomized amount of jellybeans.

Code Java:

```public class findLowestDifferencehelper {   /* *MethodName: findLowestDifference() *Purpose: determines the difference between actual Jellybeans and players' guess *Accepts: an array of type int *Returns: the difference */ public static int findLowestDifference(int num1, int num2) { int lowestDifference = Math.abs(num1 - num2);   return lowestDifference;```

Now my problem is, when i run the program, and enter let's say 2 users, and enter their guesses, i only get a difference for the first user.

```	    Welcome to Moe's Jelly Bean Guessing Game!
******************************************
Can you guess correctly how many Jelly Beans are in the jar ?
-The number of Jelly Beans in the jar ranges from 1001 to 1999-

How many people will be attempting to guess the right number today ? 3
Enter name of person #1:Moe
Moe, Enter your guess between 1000 and 2000: 1300
[*Moe has entered '1300' as a guess*]
Enter name of person #2:Jon
Jon, Enter your guess between 1000 and 2000: 1500
[*Jon has entered '1500' as a guess*]
Enter name of person #3:Steve
Steve, Enter your guess between 1000 and 2000: 1800
[*Steve has entered '1800' as a guess*]

There were 1008 Jelly beans in the jar
Your guess was 292 jelly beans away from the actual number.
```

I need to create a reportWinner() method, however, how do i make my code give me the lowestdifference for each and every guess ?

This is what the program is supposed to look like when run when i finish.

```How many players will be making guesses today? 3
Please enter name # 1: Mike
Mike, enter your guess between 1000 and 2000: 1585
Please enter name # 2: Bill
Bill, enter your guess between 1000 and 2000: 1376
Please enter name # 3: Tony
Tony, enter your guess between 1000 and 2000: 1723

There were 1139 jelly beans in the jar.

The winner is Bill with a guess of 1376, which is 237 away from the actual number of jelly beans.
```

Thanks in advance guys, I hope I didn't post too much at once ahah.
• November 29th, 2010, 08:57 AM
KevinWorkman
Re: [Method] Help ?
You're only checking against the first user:

Code :

`int lowestDifference = findLowestDifferencehelper.findLowestDifference(jellyBeans, guessArray[0]);`
• November 29th, 2010, 06:45 PM
hmafia
Re: [Method] Help ?
Quote:

Originally Posted by KevinWorkman
You're only checking against the first user:

Code :

`int lowestDifference = findLowestDifferencehelper.findLowestDifference(jellyBeans, guessArray[0]);`

Hmm okay i see , how would I check against all users ?
• November 29th, 2010, 09:22 PM
hmafia
Re: [Method] Help ?
Someone just help real quick please
• November 29th, 2010, 09:40 PM
aussiemcgr
Re: [Method] Help ?
You need to loop through the guessArray Array and check it with each value.

This part of your statement above is what restricts it to just the first value:
Code java:

`guessArray[0]`

Instead, you need loop through the guessArray and compare with each value. I can't really be bothered to read through your code since I'm tired as hell, but I'll provide some code on how you would loop through the array:
Code java:

```/* Use a FOR LOOP to go through the array. * You want to continue to go through the * array until you reach the end. This is * checked by the statement guessArray.length */ for(int i=0;i<guessArray.length;i++) { /* Now you just have to access each * value. To do this, access the array * dynamically by using the FOR LOOP * variable: i */ System.out.println(guessArray[i]); }```
This code will just print out the value in each array index, but the ability to access the spots dynamically is demonstrated accurately inside the parameters of the println method.

Hope that helps.
• November 29th, 2010, 10:14 PM
hmafia
Re: [Method] Help ?
Alright I think I get it, thanks a lot !
• November 30th, 2010, 08:14 AM
KevinWorkman
Re: [Method] Help ?
Quote:

Originally Posted by hmafia
Someone just help real quick please

That's pretty rude, and you're lucky somebody felt like helping you this time. For the future, I'd recommend you show us what you think the answer is, preferably in SSCCE form, when asking a question like that. Show some effort.
• November 30th, 2010, 08:49 AM
hmafia
Re: [Method] Help ?
Quote:

Originally Posted by KevinWorkman
That's pretty rude, and you're lucky somebody felt like helping you this time. For the future, I'd recommend you show us what you think the answer is, preferably in SSCCE form, when asking a question like that. Show some effort.

Are you joking ?

Did you even read my code ? How much effort did I put in ? You act like i came here expecting someone to do my homework for me, I wrote my method and everything, all I asked for was how would I make it possible to evaluate all guesses. I didn't even ask for code... aussiemcgr was just nice enough to show me, I had a clue that i needed a loop.

Thanks for your help though, i'm rude eh ? ;)
• November 30th, 2010, 09:34 AM
KevinWorkman
Re: [Method] Help ?
Quote:

Originally Posted by hmafia
Are you joking ?

Did you even read my code ? How much effort did I put in ? You act like i came here expecting someone to do my homework for me, I wrote my method and everything, all I asked for was how would I make it possible to evaluate all guesses. I didn't even ask for code... aussiemcgr was just nice enough to show me, I had a clue that i needed a loop.

Thanks for your help though, i'm rude eh ? ;)

Excellent. I did read your code. In fact, I pointed out exactly what you were doing wrong.

You could have said something like, "oh, I see my error now, and I think maybe if I did XYZ, I could fix my problem. But when I tried that, ABC happened instead. Here is some code that demonstrates where I'm stuck."

Instead, you took the lazy way out, without showing any effort or even enough effort to use basic punctuation. You're lucky you received the help you did.

ignoreList++.
• November 30th, 2010, 06:22 PM
hmafia
Re: [Method] Help ?
Quote:

Originally Posted by KevinWorkman
Excellent. I did read your code. In fact, I pointed out exactly what you were doing wrong.

You could have said something like, "oh, I see my error now, and I think maybe if I did XYZ, I could fix my problem. But when I tried that, ABC happened instead. Here is some code that demonstrates where I'm stuck."

Instead, you took the lazy way out, without showing any effort or even enough effort to use basic punctuation. You're lucky you received the help you did.

ignoreList++.

After you pointed out what I did wrong I went back to my code and did a bunch of trial and error.

I didn't bother to come back and post what i've tried because I was too busy with assignments, programming isn't the only course *** me right now. It's crunch time. I came back on here to check my post and aussie just so happened to post some more stuff, so i went ahead with that.

You got offended over nothing. I don't care if you have a procedure that you think every new member should follow. Have some decency in you.Your attitude stinks, all you got is 150 posts and you have the guts to talk. Give me a break, show respect to gain respect.
• November 30th, 2010, 07:32 PM
copeg
Re: [Method] Help ?
I would like to remind the users of this thread to read the rules for posting in the forums. We are all here to learn and to help, and at times the context through which we do so can be impersonal, sometimes causing things to easily get taken out of context - please respect the other members of the forum and keep this in mind when posting comments directed at other users.
• November 30th, 2010, 09:28 PM
hmafia
Re: [Method] Help ?
Well said, again, thanks for everyones help. :)

Not here to slack off, i'm paying money to learn programming after all !