james
james

Reputation: 27

Java code what is wrong with this?

I am really new to programming, on netbeans i have deleted all the other text, all i have is the following, Why wont the program run?

The error i get is, no main Class found.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package findcost2;
 public class Main 

 /* a program to calculate and display the cost of a product after 
  * sales tax has been added*/

public class FindCost2
        Public static void main (String[] args)
{
      double price,tax;
      price = 500;
      tax = 17.5;
      price = price * (1 + tax/100);// calculate cost
      // display results
      System.out.println("***Product Price Check");
      System.out.println("Cost after tax = " + price);
    }
}

Upvotes: 0

Views: 314

Answers (4)

Mike Adler
Mike Adler

Reputation: 1200

Numerous problems with this code:

  1. The outer class (Main) does not have an opening bracket. Insert the { bracket.

  2. The inner class (FindCost2) does not have an opening bracket. Insert the { bracket.

  3. The public modifier for the main method is capitalized. Start with a lowercase p.

  4. The main method is nested in an inner class. This is really bad form. To make it work anyway, the inner class needs to be static. Insert the static keyword.

When put like this, it compiles:

public class Main {

/*
 * a program to calculate and display the cost of a product after sales tax
 * has been added
 */

public static class FindCost2 {
    public static void main(String[] args) {
        double price, tax;
        price = 500;
        tax = 17.5;
        price = price * (1 + tax / 100);// calculate cost
        // display results
        System.out.println("***Product Price Check");
        System.out.println("Cost after tax = " + price);
    }
}
}

However, there is absolutely no point to the outer class (Main). Just delete this. When the outer class is removed, the inner class (FindCost2) need not be static anymore. Remove the keyword.

It is really bad form to declare multiple variables on one line (as in double price, tax;). Split that to two lines:

double price;
double tax;

There are good reasons not to use the double type for monetary values. With a little extra work, you can easily write a simple Money class. Check javapractices.com for a good overview on that.

Hope that helps!

Upvotes: 1

r0ast3d
r0ast3d

Reputation: 2635

Try this exactly and name your java file FindCost2.java

package findcost2;

public class FindCost2{

public static void main (String[] args)
{
  double price,tax;
  price = 500;
  tax = 17.5;
  price = price * (1 + tax/100);// calculate cost
  // display results
  System.out.println("***Product Price Check");
  System.out.println("Cost after tax = " + price);
}

}

Upvotes: 3

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

Why is Public capitalized? Shoud be:

public class FindCost2 {
    public static void main(String[] args) { ... }
}

Upvotes: 1

Mike Samuel
Mike Samuel

Reputation: 120516

You're missing a curly bracket after class Main and you have two public classes in the same source file. Delete public class Main and change Public to public.

You should probably also use decimal numbers for dealing with currencies

Sooner or later, everyone trying to calculate money in Java discovers that computers can't add.

Upvotes: 2

Related Questions