Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jennifer Barbee
Front End Web Development Techdegree Student 2,387 PointsHow come no one will help me with my question? I'm stuck and I need some help.
Hi, I've been reaching out for a while now. I'm not sure what I'm doing wrong as my program is running fine. However, when I try to fill my jar with a random number between 1 and the maxNumber, it doesn't work. I let the user guess the randomNumber, and then return how many guesses it took once they guess it. However, sometimes the result is 0 when it shouldn't be any lower than 1.
For testing purposes, I make the ma number 5 each time. Now, lets say we run the program and the randomNumber is '3'. Sometimes my program won't catch '3' when it is guessed the first time, and 3 has to be guessed again for my loop to catch it (example guesses: 5,4,3,2,1,5,4,3). You can see that it should caught it with 3 guesses, but when it does this you can also see where it is returning 8 guesses because it doesn't catch it until the second 3.
I don't get it or see where it would be skipping the correct number the first time. Why does it return 0 as the max Number sometimes? Can someone please help? It's very disheartening to stay stagnant on a project for this long :(
MAIN CLASS
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
System.out.println("ADMINISTRATOR SETUP\n********************");
System.out.println("GUESSING GAME: Guess how many items are in the jar.\n\nSelect the type of items, and how many can reasonably fit in a jar.\nSound good? Okay let's begin!\n");
if(args.length != 2) {
Jar jar = new Jar();
jar.fillJar();
}
}
}
JAR CLASS
import java.util.Scanner;
import java.util.Random;
public class Jar {
private String itemName;
public int maxItems;
public Jar() {
Scanner scanner = new Scanner(System.in);
System.out.println("What type of item?");
this.itemName = scanner.nextLine();
System.out.println("What is the maximum amount of " + itemName + " ?");
this.maxItems = Integer.parseInt(scanner.nextLine());
System.out.println("\nPLAYER\n********************\nHow many " + itemName + " are in the jar?\nPick a number between 1 and " + maxItems);
}
public String fillJar() {
Random randomNumber = new Random();
int rand = randomNumber.nextInt(this.maxItems)+1;
Scanner scanner = new Scanner(System.in);
int guess = Integer.parseInt(scanner.nextLine());
int count = 1;
do {
Scanner scanner1 = new Scanner(System.in);
System.out.println("\nHow many " + itemName + " are in the jar? Pick a number between 1 and " + maxItems);
guess = Integer.parseInt(scanner1.nextLine());
count ++;
if(guess==rand){
System.out.println("You got it in " + count + " attempt(s)");
}
} while(guess != rand);
return "You got it in X attempt(s)";
}
}
Thank you very much for reviewing my post!! -Jen
Steve Hunter
57,712 PointsThis seems to be working fine for me. It doesn't handle a blank or string input well during the loop and there's no prompting if you guess wrongly but it works fine.
What scenario did you create that caused an issue?
Steve.
Jennifer Barbee
Front End Web Development Techdegree Student 2,387 PointsThanks Steve, and no problem. I apologize on the mistake you were talking about as well. I accidently posted the wrong version of my workspace but I updated it. --it was supposed to be :
int rand = randomNumber.nextInt(this.maxItems)+1;
As far as the rest of my code, here is what we are looking at:
public String fillJar() {
Random randomNumber = new Random();
int rand = randomNumber.nextInt((this.maxItems - minItems)+1);
Scanner scanner = new Scanner(System.in);
int guess = Integer.parseInt(scanner.nextLine());
int count = 1;
do {
Scanner scanner1 = new Scanner(System.in);
System.out.println("\nHow many " + itemName + " are in the jar? Pick a number between 1 and " + maxItems);
guess = Integer.parseInt(scanner1.nextLine());
count ++;
if(guess==rand){
System.out.println("You got it in " + count + " attempt(s)");
}
} while(guess != rand);
return "You got it in X attempt(s)";
}
The problem I'm having is in my fillJar() method. I want the jar to be filled with a random number of items between 1 and the variable I created (public int maxItems). So, if maxItems = 50 then I want my random number to be between 1 and 50. Next I allow the user to guess the random number. As soon as they guess it correctly they should be prompted that they guessed it, and told how many guesses it took. This works, but returns inaccurate information.
Jennifer Barbee
Front End Web Development Techdegree Student 2,387 PointsSteve,
Like I said, it runs fine for me. But try running it 5 times with the maxItem set for 5 each time and you see what I'm talking about. Sometimes it catches it on the first try, but other times it doesn't--unless there is something wrong with my console.
When it "glitches" my guesses would be 1,2,3,4,5... since none of them worked by sixth guess would be 1 then 2 then 3...making '3' my third and 8th guess. Sometimes the program CORRECTLY stops at three guesses, but sometime it waits until I guess it a second time. Does that make sense? Because it is making no sense to me :(
Jennifer Barbee
Front End Web Development Techdegree Student 2,387 PointsHey Steve, all of your suggestions worked and I fixed my count as well to return the correct number (count = 0 instead of count = 1). I also found that the random number was still returning 0 sometimes, so I updated my code to fix it. I think I have it now. fingers crossed I appreciate your help and I especially appreciate your notes in the code. Everything made sense. Thanks again!
public void fillJar() {
Scanner scanner1 = new Scanner(System.in);
Random randomNumber = new Random();
int rand = randomNumber.nextInt((this.maxItems - minItems)+1);
if(rand==0){ // I added this to catch when the random number equals 0, and make it equal 1 instead
rand = rand +1;
}
int guess;
int count = 0;
do {
System.out.println("\nHow many " + itemName + " are in the jar? Pick a number between 1 and " + maxItems);
guess = Integer.parseInt(scanner1.nextLine());
count++;
if(guess==rand){
System.out.println("You got it in " + count + " attempt(s)");
}
} while(guess != rand);
}
Steve Hunter
57,712 PointsGlad to hear you're making progress. Good work!
Let me know how you get on - and what's minItems for?
Steve.
1 Answer
Steve Hunter
57,712 PointsI've added some comments - the key change here is the introduction of minItems what is that? The rest is just to tidy up the code as there's some unused stuff in there:
public String fillJar() { // return void, the string isn't used
Random randomNumber = new Random();
int rand = randomNumber.nextInt((this.maxItems - minItems)+1); // what's minItems? And why is it used?
Scanner scanner = new Scanner(System.in); // this Scanner object is never used
int guess = Integer.parseInt(scanner.nextLine()); // just create 'int guess;' The rest is redundant
int count = 1;
do {
Scanner scanner1 = new Scanner(System.in);
System.out.println("\nHow many " + itemName + " are in the jar? Pick a number between 1 and " + maxItems);
guess = Integer.parseInt(scanner1.nextLine());
count ++; // remove the space before ++
if(guess==rand){
System.out.println("You got it in " + count + " attempt(s)");
}
} while(guess != rand);
return "You got it in X attempt(s)"; // unnecessary line - returned value not used
}
Let me know about minItems that's messing with the range of numbers. Do you think count is reporting bad figures? What do you refer to as "inaccurate information"?
Steve.
P.S. I need to head out now - I'll be back in a couple of hours.
Steve Hunter
57,712 PointsHi Jennifer,
I'm back in the office now - can you update me on the minItems thing? I can then get on with figuring out what's up with your code. My answer above refers in the body of the post and comments in the code.
Thanks,
Steve.
Jennifer Barbee
Front End Web Development Techdegree Student 2,387 PointsThanks Steve. I would assume minItems is needed because I'm looking for a range. However, I still don't understand why it returns 0 sometimes, but I did figure out how to fix it. So that's half the battle
Steve Hunter
57,712 PointsHiya,
The minItems variable is not defined within the code I have seen. It is used but not defined. When it is used it is subtracted from this.maxItems.
So, don't assume what it does, this is your code! Where is it declared and initialized; what is assigned to it, and why is it deducted from maxItems?
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi Jennifer,
Apologies, I hadn't seen your previous posts.
I'll have a look and see if I can figure out the issue with your code. Which variables are holding the wrong value or are not picking up on what you're expecting them to? I can make sure to keep an eye on those.
I'll have a look at your code and see what results I get with it.
Steve.