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

Thread: [Method] Help ?

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default [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.

    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.


    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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: [Method] Help ?

    You're only checking against the first user:

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

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [Method] Help ?

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

    int lowestDifference = findLowestDifferencehelper.findLowestDifference(jellyBeans, guessArray[0]);
    Hmm okay i see , how would I check against all users ?

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [Method] Help ?

    Someone just help real quick please

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default 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:
    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:
    /* 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.
    Last edited by aussiemcgr; November 29th, 2010 at 10:42 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    hmafia (November 29th, 2010)

  7. #6
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [Method] Help ?

    Alright I think I get it, thanks a lot !

  8. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: [Method] Help ?

    Quote Originally Posted by hmafia View Post
    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.

  9. #8
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [Method] Help ?

    Quote Originally Posted by KevinWorkman View Post
    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 ?

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default Re: [Method] Help ?

    Quote Originally Posted by hmafia View Post
    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++.

  11. #10
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [Method] Help ?

    Quote Originally Posted by KevinWorkman View Post
    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.
    Last edited by copeg; November 30th, 2010 at 08:37 PM. Reason: Removed inflammatory language

  12. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default 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.

  13. #12
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [Method] Help ?

    Well said, again, thanks for everyones help.

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

Similar Threads

  1. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 12:18 PM