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

ousmane Harouna Kore
ousmane Harouna Kore
2,698 Points

Failed to compile

Hello I submitted my project 1 for review and I received a feedback "failed to compile in src/main/java/Jar.java" can someone please tell me what is wrong with my code despite that it working properly in the workspaces. Thanks

2 Answers

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

Hello again,

Took a quick look, your class doesnt have acces modifier (private/public/protected). There is 2 constructors for Jar, one empty and other is properly declared.

-Alex

Alex Bratkovskij
Alex Bratkovskij
5,329 Points

Heya Harouna,

We will be able to help you if you provide us with the source code :)

-Alex

ousmane Harouna Kore
ousmane Harouna Kore
2,698 Points

Hello Mr Alex please find below my code. Thanks

ousmane Harouna Kore
ousmane Harouna Kore
2,698 Points
import java.util.Scanner;
import java.util.Random;
class Jar{
//declare variables  
private String itemName;
private int maxItems;
Scanner scan=new Scanner(System.in);
  //add constructor
 public Jar(){

 }

public Jar(String itemName, int maxItems ){
 this.itemName=itemName;
 this.maxItems= maxItems;
} 
  //normalize items name
 public String normalizeItemsName(String itemName){
 if(itemName.length()==0){
   throw new IllegalArgumentException("No itemName found");
  }
 for(char letter:itemName.toCharArray()){
  if(!Character.isLetter(letter)){
    throw new IllegalArgumentException("an item name made with letters is required");
  }
 }
  return itemName.toLowerCase();
}

//prompt administrator to enter the type and max numbers of items
 public void identifyItems(){
  boolean isAcceptable=false; 

 System.out.println("ADMINISTRATOR SETUP");
 System.out.println("--------------------");
  do{
 System.out.println("What type of item should go in the jar:");
 itemName= scan.nextLine();
  try{
    itemName=normalizeItemsName(itemName);
    isAcceptable=true;
  }catch(IllegalArgumentException iae){
    System.out.printf("%s. Please try again. %n",
                   iae.getMessage());
  }
  }while(!isAcceptable);

 do{
 System.out.printf("What is the maximum amount of %s that should go in the jar:",itemName);
   try{
      maxItems=Integer.parseInt(scan.nextLine());
     isCorrectNum=true;
   }catch(Exception iae){
      System.out.printf("invalid input. Please try again. %n");
   }
  }while(!isCorrectNum);
 System.out.println("--------------------.\n\n");
}
  //fill the jar with a random number
 public int fill(){
   Random rand=new Random();
   //random.nextInt(max-min+1)+min
   int randomNumber=rand.nextInt(maxItems)+1;
   return randomNumber;
 } 
  //guessing method 
  public void guessItem(){
    System.out.println("--------------------");
    System.out.println("Welcome to the guessing game!");
    boolean isCorrect=false;
    int targetNum=fill();
    int numOfTries=0;
    do{
 System.out.printf("How many %s are in the jar? Pick a number between 1 and %d :",itemName,maxItems);
      ++numOfTries;
      int guessInput=scan.nextInt();
      if(guessInput==targetNum){
        System.out.printf("Congrats! You got it in %d attempts!.%n",numOfTries);
       isCorrect=true;
      }
    }while(!isCorrect);

  }
}