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

Java Guess How Many Are In The Jar Game in Java

Gendarme Docena
Gendarme Docena
1,509 Points

I'm stuck on the part where the user presses the "Enter" key

I've looked around stackoverflow and youtube but the ways the programmers taught it all seemed very different. Any suggestions please? This is what I have so far:


public class TreeHouse1 {

    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in);

        String itemName;
        System.out.println("Name of items in the jar:");
        itemName = input.next();

        System.out.printf("Maximum number of %s in the jar: ", itemName);
        int integer = input.nextInt();

        System.out.printf("Your goal is to guess how many %s is in the jar. Your guess should be between 1 and %d. \n ", itemName, integer);
        System.out.println("Ready? (Press ENTER to start guessing)");



        System.out.println("Guess:  ");
        int guess = input.nextInt();
        Random random = new Random();
        int someNumber = random.nextInt(maxItemsAllowed); // Some number btwn 0&20

[MOD: edited code block - srh]

1 Answer

Hi there,

If you push your code to Github and then share that on the Java Techdegree Slack channel, someone will help you there.

One thing; I can't see that you've defined maxItemsAllowed anywhere? You've used it, but the code doesn't know what it is.

Good luck!

Steve.