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

Mélodie 95
Mélodie 95
813 Points

I need help to improve this code

Hi everyone , i'm currently working on my first Java project and i got some problems with this code. When i run the program , it asks for the type , but i can't give it an input , can someone help me figure out why? Any tips and advice concerning the design and some other things in my code is highly appreciated . Thanks in advance.

import java.util.Random;
public class Jar {

    private int numberOfItems;
    private int maxNumberOfItems; 
    private String type;

    public int getNumberOfIntems(){
        return numberOfItems;
    }
    public int getMaxNumberOfItems(){
        return maxNumberOfItems;
    }
    public String getType(){
        return type;
    }
    public Jar(){

    }
    public Jar(int maxNumberOfItems , String type){
        this.maxNumberOfItems = maxNumberOfItems;
        this.type = type;
    }

    public void fill(){
        Random random = new Random();
        numberOfItems = random.nextInt(maxNumberOfItems) + 1 ;

        }

    }
import java.util.Scanner;
public class Administrator {

    Prompter prompter = new Prompter();
    Scanner scanner = new Scanner(System.in);

    public int setMaxNumber(){
        prompter.promptTheAdministratorForMaxNumber();
        int MaxNumb = scanner.nextInt();
        return MaxNumb;
    }

    public String setType(){
        prompter.promptTheAdministratorForType();
        String type = scanner.nextLine();
        return type;
    }
}
import java.util.Scanner;
public class Player {

    Scanner scanner = new Scanner(System.in);

    private int numberOfGuesses;
    private int guess ;

    public int getNumberOfGuesses(){
        return numberOfGuesses;
    }
    public void guess(){
        Prompter prompter = new Prompter();
        numberOfGuesses = 0 ;
        while(guess != prompter.jar.getNumberOfIntems()){
        prompter.promptForGuess();
        guess = scanner.nextInt();
        if(guess < 1 ){
            throw new IllegalArgumentException("Your guess is too low");
        }else if (guess>prompter.jar.getMaxNumberOfItems()){
            throw new IllegalArgumentException("Your guess is too high");
        }else if (guess==prompter.jar.getNumberOfIntems()){
            prompter.displayNumberOfGuesses();
        }
        else{
        numberOfGuesses++;
          }
        }
public class Prompter {

    public Jar jar = new Jar();
    public Player player  = new Player();



    public void promptForGuess(){
        System.out.println("How many items are in the jar?");   
    }

    public void promptTheAdministratorForMaxNumber(){
        System.out.println("What is the maximum amount of items?");

    }
    public void promptTheAdministratorForType(){
        System.out.println("What type of item?");
    }

    public void displayNumberOfGuesses(){
        System.out.printf("You got it in %d attemps", player.getNumberOfGuesses() );
    }   
}
public class Game {

    Administrator admin = new Administrator();

    Jar jar = new Jar(admin.setMaxNumber() , admin.setType());  

    public void setGame(){
        jar.fill();
        System.out.printf("The Maximum number of %s in the jar is %d",jar.getType() , jar.getMaxNumberOfItems());
    }

    public static void main(String[]args){
        Prompter prompter = new Prompter();
        Player player = new Player();
        Game game = new Game();
        game.setGame();
        prompter.promptForGuess();
        try{
        player.guess();
        }catch(IllegalArgumentException iae){
            System.out.println("Please try again");
        }
    }   
}

[MOD: edited code blocks - srh]

1 Answer

Hi Melodie,

I would recommend uploading your code to a Github repo and allowing users to pull your code down to replicate the error locally.

I'll edit your post to show the code blocks in a better format too.

Steve.