0

I'm trying to compile my main class, Halloween, but I'm getting an error that says that the class TreatHouse is not found. I'm not sure why this is happening, but would anybody be able to help me out please? Also, please feel free to comment on any other errors you may see in my code; any help is greatly appreciated. Thanks!

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Halloween {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        ArrayList<TreatHouse> houses = new ArrayList<TreatHouse>();

        Random gen = new Random(System.currentTimeMillis());
        int numHouse = gen.nextInt(10) + 1;

        // create houses
        for (int i = 0; i < numHouse; i++) {
            System.out.println("House " + i );
            System.out.println("Which candy should we give out first? Candy in pot 1 or candy in pot 2?");
            int candyPot = scan.nextInt();

            System.out.println("How much candy did we buy?");
            int totalCandy = scan.nextInt();

            houses.add( new TreatHouse(candyPot, totalCandy));
        }

        boolean allDone = false;
        while(!allDone){
            //Each house will pass out candy for a bit.
            for (TreatHouse house : houses) {
                house.passTime();               
            }

            //Check to see if all the house are done. If so set sentinel flag to true and exit while loop
            allDone = false;
            for(TreatHouse house: houses){
                //If a single house is not done then all are not done
                if(!house.isDone()){
                    allDone = false;
                    break;
                }
                else
                    allDone = true;
            }

            //Call static method to display some statistics about the holiday so far


        }
        System.out.println("All houses are done");
        scan.close();
    }
}

import java.util.Random;
import java.util.Scanner;

public class TreatHouse {
    private int candyPot1; // amount of candy in pot 1 for a single TreatHouse
    private int candyPot2; // amount of candy in pot 2 for a single TreatHouse
    private int currentPot; // (1 or 2) current Pot for a single TreatHouse
    private int totalCandy; // total candy for a single TreatHouse
    private int currentTreaters; // current Treaters for a single TreatHouse
    private boolean isDone = false; // is this house done for the night?
    private int treatsPerTreater; //How much candy to give out to each treater at a single TreatHouse
    private int nextID;

    //Static variables
    public static int participatingHouses = 0; //This contains the total number of houses participating in this year's holiday
    public static int totalCandyPassedOut = 0; //This contains the total number of candy passed out for all the houses.
    public static int totalTricksGiven = 0;    //This contains the total number of tricks given out. Remember that each kid gets tricked.
    public static int houseID = 1;              //This is the number for the house which is unique.

   //Constructor
   public TreatHouse()
   {
       participatingHouses++;
       totalCandyPassedOut++;
       totalTricksGiven++;
       nextID=houseID++;
   }      
    //Method to display the statistic variables
   public static void getHalloweenStatus()
   {
      System.out.println("Total participating houses: " + participatingHouses); //Displays the total number of participating houses
      System.out.println("Total candy passed out: " + totalCandyPassedOut);     //Displays the total amount of candy passed out 
      System.out.println("Total tricks given: " + totalTricksGiven);            //Displays the total amount of tricks given
      System.out.println(houseID);  
    }

    public TreatHouse(int candyPot, int totalCandy) {
        // Error Check and split candy
        if (totalCandy > 0) {
            candyPot1 = totalCandy / 2;
            candyPot2 = totalCandy / 2 + totalCandy % 2;
        } else {
            System.out.println("We can't give out candy if we don't have any."
                    + " I think we have some from last year. Yep, we have 100 " + "" + "pieces of candy to give out.");
            totalCandy = 100;
            candyPot1 = totalCandy / 2;
            candyPot2 = totalCandy / 2 + totalCandy % 2;
        }

        // currentPot Check
        if (candyPot == 1) {
            currentPot = 1;
        } else if (candyPot == 2) {
            currentPot = 2;
        } else {
            System.out.println("Invalid choice for pot. Only 1 or 2. Using pot 1");
            currentPot = 1;
        }

        //Adjust Static variables as needed here:


    }

    public void passTime() {
        Scanner scan = new Scanner(System.in);

        //before
        this.getCandyStatus();

        System.out.println("How much candy per treater should we give out?");
        this.setTreatsPerTreater(scan.nextInt());

        System.out.println("Knock, knock...." + "Trick or treat!");
        this.knockKnock();
        this.passOutCandy();

        //After
        this.getCandyStatus();

        //Determines if the house if done for the night.
        if(totalCandy == 0){
            System.out.println("Time to turn off the lights and go to bed!");
            System.out.println("The last candy came from pot number" + this.getLastPot());
            System.out.println("Happy Halloween!");
            isDone = true;
        }
    }

    private void printCurrentPot() {
        System.out.println("The current Pot is: " + currentPot);        
    }



    public int getCandyCount() {
        return candyPot1 + candyPot2;
    }

    public void passOutCandy() {
        // If there are enough treats per treater for the given amount per
        // treater, pass out candy from the current pot and switch to the other one.
        // Else display a message that the treaters have been tricked... (no
        // candy for them.) but do not change the current pot

        if ((currentPot == 1 && candyPot1 == 0) || (currentPot == 2 && candyPot2 == 0)) {
            // Switch pots and print message
            System.out.println("There is no candy in the currentPot Switching pots.");
            switchPots();
        }

        {// There is some candy to give out.
            int currentPotAmmount = getCurrentPotAmount();
            if (treatsPerTreater * currentTreaters <= currentPotAmmount) {
                //Passing out Candy
                if (currentPot == 1) {
                    candyPot1 -= treatsPerTreater * currentTreaters;
                    totalCandy -= treatsPerTreater * currentTreaters;
                } else {
                    candyPot2 -= treatsPerTreater * currentTreaters;
                    totalCandy -= treatsPerTreater * currentTreaters;
                }
                switchPots();
            } else {
                // trick
                System.out.println("You have been tricked!");
            }

        }

    }

    private int getCurrentPotAmount() {
        if (currentPot == 1)
            return candyPot1;
        else
            return candyPot2;
    }

    private void switchPots() {
        if (currentPot == 1) {
            currentPot = 2;
        } else
            currentPot = 1;

    }

    // Sets the number of trick or treaters.
    public void knockKnock() {
        Random gen = new Random(System.currentTimeMillis());
        this.currentTreaters = gen.nextInt(13) + 1; // 1 to 13 treaters.
        System.out.println("There are " + currentTreaters + " treaters.");
    }

    // Displays how much candy in each pot, total candy left

    public void getCandyStatus() {
        this.printCurrentPot();
        System.out.println("Candy in Pot 1: " + candyPot1);
        System.out.println("Candy in Pot 2: " + candyPot2);

    }

    // returns the pot number for which candy was last given.
    public int getLastPot() {
        if (currentPot == 1)
            return 2;
        else {
            return 1;
        }
    }

    public void setTreatsPerTreater(int treatsPerTreater) {
        this.treatsPerTreater = treatsPerTreater;
    }

    public boolean isDone() {

        return isDone;
    }
}
salmon1407
  • 17
  • 1
  • 5

2 Answers2

1

Assuming that you have saved the two classes (Halloween and TreatHouse) in different java files, usually "Symbol not found" exception will come if java compiler is unable to find the class file (in your case TreatHouse)

If the two java files are in same folder then when you compile Halloween.java it will automatically compile TreatHouse.java, if the java files is in different folder you need to compile TreatHouse.java first and set the classpath to TreatHouse.class location later compile Halloween.java

or

If you are having both class in same java file,

Remove public access modifier from of one of the class since we can have only one public class in a java file and remove the import statement in the middle and save the file with the public class name

eg.

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Halloween
{
//code
}

class TreatHouse
{
//code
}

The file name for the above code should be Halloween.java After compiling Halloween.java (javac Halloween.java) you will get two class files Halloween.class and TreatHouse.class

0

You seem to have two copies of your class there... halfway down you have another set of imports (which can only ever be at the top of a file) and another TreatHouse class...

After pasting your code into an IDE and removing the second half I have no compile errors.

Numeron
  • 8,723
  • 3
  • 21
  • 46
  • 1
    Well you still have two classes there, they need to be separated into their own file, Halloween.java and TreatHouse.java There are no compile errors in the code itself. – Numeron Nov 16 '15 at 05:22
  • ... And actually TreatHouse is still in there twice too – Numeron Nov 16 '15 at 05:23
  • Okay, I removed the second `TreatHouse`. However, the `Halloween` class is the driver class for `TreatHouse`, and I'm having a problem compiling `Halloween` because it says that the symbol "TreatHouse" is not found. `TreatHouse` compiles fine though. – salmon1407 Nov 16 '15 at 05:39
  • Are you using the command line to compile the files one at a time instead of through an IDE then? If so then this question/answer is very good: – Numeron Nov 16 '15 at 05:46
  • ---------- The code sample you have posted shows no import tld.yoursomething.TreatHouse; line near the beginning. Such a line is required in your Halloween.java file before the class Halloween declaration so the compiler can find the correct package subdirectory and TreatHouse.class file to examine to learn all about the class TreatHouse. In summary: ** Java requires that every .java source file have 1 and only one outermost class declaraction, and that the class name be identical to the Something.java filename. ------------- – TSnyder Nov 16 '15 at 06:33