0

I'm very new to Java and I keep getting this error and I can't figure it out.

Error: Could not find or load main class restaurantclient.RestaurantClient Java Result: 1

This is on Netbeans and I'm sure I've done everything right. This is for my homework for a Java class and I would really appreciate your help. I have been working on this problem for days now.

import java.text.DecimalFormat;

public class RestaurantClient {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Restaurant r1;
        Restaurant r2;
        r1 = new Restaurant("PizzaHut", 152, (float) 4.95); //instantiating
        r2 = new Restaurant("Dominos", 10, (float) 3.657);
        System.out.print(r1.toString());
        System.out.print(r2.toString());
        r2.setPeopleServed(r1.getPeopleServed());
        r2.setAveragePrice(r1.getAverageMeal());
        if (r1.equals(r2)) //checking if equal
            System.out.println("\nThe two objects are the same");
        else
            System.out.println("\nThe two objects are NOT the same");
        r2.setName(r1.getName());
        if (r1.equals(r2)) //checking if equal
            System.out.println("\nThe two objects are the same");
        else
            System.out.println("\nThe two objects are NOT the same");
        DecimalFormat pricePattern = new DecimalFormat("$###,###,000.00");
        System.out.println("Tax paid per year: " + pricePattern.format(r1.averageTaxes()));

    }

}

//Restaurant.java

public class Restaurant extends Store {
    private int peopleServed;
    private float avgmeal;

    public Restaurant(String newName, int peopleServed, float avgmeal) {
        super(newName);
        setPeopleServed(peopleServed);
        setAveragePrice(avgmeal);
    }
    public void setPeopleServed(int newpeopleServed) {
        if (newpeopleServed >= 0)
            peopleServed = newpeopleServed; //mutator alows client to change the value of name
        else
            System.out.println("Number has to be greater than zero");
    }
    public void setAveragePrice(float newprice) {
        if (newprice >= 0)
            avgmeal = newprice; //mutator alows client to change the value of name
        else
            System.out.println("Number has to be greater than zero");
    }
    public int getPeopleServed() {
        return peopleServed; //accessor; returns current value
    }
    public float getAverageMeal() {
        return avgmeal; //accessor; returns current value
    }
    public String toString() {
        return "Name of store is:   " + super.getName() +
            "\nNumber of people served is   " + peopleServed +
            "\nAverage price per person is " + avgmeal + "\n";
    }

    public boolean equals(Object o) {
        if (!(o instanceof Restaurant))
            return false;
        else {
            Restaurant objRest = (Restaurant) o;
            if (avgmeal == objRest.avgmeal && peopleServed == objRest.peopleServed && super.equals(objRest))
                return true;
            else
                return false;

        }

    }
    public double averageTaxes() {
        double avgtaxes = (double)(super.taxrate * (peopleServed * avgmeal * 365));
        return avgtaxes; //accessor; returns current value
    }

}

//Store.java

public class Store {
    private String storename;
    public final float taxrate = (float) .08000;

    public Store(String newName) //constructor
    {
        setName(newName);
    }

    public String getName() {
        return storename; //accessor; returns current value
    }
    public void setName(String newName) {
        storename = newName; //mutator alows client to change the value of name
    }
    public String toString() {
        return "Name of store is:   " + storename;
    }
    public boolean equals(Object o) {
        if (!(o instanceof Store))
            return false;
        else {
            Store objStore = (Store) o;
            if (storename.equals(objStore.storename))
                return true;
            else
                return false;
        }
    }
}
Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
timtommy
  • 289
  • 1
  • 11
  • 1
    how are you running this code form IDE or cmd? b/c it works fine for me – Syed Mehtab Hassan May 10 '19 at 06:16
  • If you want to have the classes `Restaurant` and `Store` in the same class file, then you need to remove `public` from the class definition. Code compiles fine if you do that. – eeijlar May 10 '19 at 06:21
  • 1
    @eeijlar seeing as the issue asked about is runtime, I doubt compilation is a problem – Stultuske May 10 '19 at 06:23
  • @Stultuske: sorry, I meant compiles and runs fine also. I ran the OP code at a terminal with those changes : `Name of store is: PizzaHut Number of people served is 152 Average price per person is 4.95 Name of store is: Dominos Number of people served is 10 Average price per person is 3.657 The two objects are NOT the same The two objects are the same Tax paid per year: $21,970.08 ` – eeijlar May 10 '19 at 06:26

2 Answers2

2

The system is looking for a main class restaurantclient.RestaurantClient, so a class RestaurantClient in package restaurantClient, but your class RestaurantClient seems to be in the default package.

talex
  • 17,973
  • 3
  • 29
  • 66
1

try this if you are not getting any compilation issues.

You can :

  1. RightClick on project node and go to Set configuration.
  2. Select the main class for your application.
  3. Then clean and build.

Even if the above steps don't work for you then then delete the Netbeans cache by deleting the (index) folder

Fore more details Follow the stackOverflow question Netbeans - Error: Could not find or load main class

Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37