I keep getting this error any way to fix ? Code not compiling. Error: Main metho
ID: 3908805 • Letter: I
Question
I keep getting this error any way to fix ? Code not compiling.
Error: Main method not found in class MainClass, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
import java.util.Scanner;
import java.io.*;
public class MainClass
{
public class CollegeInfo
{
String college_name;
int sticker_price;
int net_price;
int discount;
//getter and setters of variables of class
public String getCollege_name()
{
return college_name;
}
public void setCollege_name(String college_name)
{
this.college_name = college_name;
}
public int getSticker_price()
{
return sticker_price;
}
public void setSticker_price(int sticker_price)
{
this.sticker_price = sticker_price;
}
public int getNet_price()
{
return net_price;
}
public void setNet_price(int net_price)
{
this.net_price = net_price;
}
//calculating discount provided by college
public void discount()
{
discount=(int)((sticker_price-net_price)/sticker_price*100);
}
//returning the discount
public int getDiscount()
{
return discount;
}
}
public class main
{
//static members created to hold values which should not change with new object creation
int avg_stick_price;//to hold average sticker price
int avg_net_price;//to hold average net price
int avg_discount;//to hold the discount
public main(String[] args)
{
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number of colleges to be entered: ");
int n=sc.nextInt();//asking user to provide length of array
CollegeInfo[] college_array=new CollegeInfo[n];//creating college array
for(int i=0;i<n;i++)//running loop till n
{
college_array[i]=new CollegeInfo();//assigning array term class
System.out.println("Enter Name of College: ");
college_array[i].setCollege_name(sc.next());//using setters to enter values
System.out.println("Enter Sticker Price: ");
college_array[i].setSticker_price(sc.nextInt());
avg_stick_price=avg_stick_price+college_array[i].getSticker_price();//using getters to calculate total for calculating average
System.out.println("Enter Net Price: ");
college_array[i].setNet_price(sc.nextInt());
avg_net_price=avg_net_price+college_array[i].getNet_price();
college_array[i].discount();
avg_discount=avg_discount+college_array[i].getDiscount();
}
System.out.format("%50s %16s %16s %16s ","College","Sticker Price","Net Price","% Discount Rate");
avg_stick_price=avg_stick_price/n;
avg_net_price=avg_net_price/n;
avg_discount=avg_discount/n;
//printing the result
for(int j=0;j<n;j++)
{
if(college_array[j].getDiscount()>=60)
{
//using string formatting
System.out.format("%50s %16s %16s %16s ",college_array[j].getCollege_name()+"**","$"+college_array[j].getSticker_price(),"$"+college_array[j].getNet_price(),college_array[j].getDiscount());
}
else if(college_array[j].getDiscount()>=50)
{
System.out.format("%50s %16s %16s %16s ",college_array[j].getCollege_name()+"*","$"+college_array[j].getSticker_price(),"$"+college_array[j].getNet_price(),college_array[j].getDiscount());
}
else
{
System.out.format("%50s %16s %16s %16s ",college_array[j].getCollege_name(),"$"+college_array[j].getSticker_price(),"$"+college_array[j].getNet_price(),college_array[j].getDiscount());
}
}
System.out.format("%50s %16s %16s %16s","Average","$"+avg_stick_price,"$"+avg_net_price,avg_discount);
}
}
}
Explanation / Answer
If you have any doubts, please give me comment....
import java.util.Scanner;
import java.io.*;
class CollegeInfo {
String college_name;
int sticker_price;
int net_price;
int discount;
// getter and setters of variables of class
public String getCollege_name() {
return college_name;
}
public void setCollege_name(String college_name) {
this.college_name = college_name;
}
public int getSticker_price() {
return sticker_price;
}
public void setSticker_price(int sticker_price) {
this.sticker_price = sticker_price;
}
public int getNet_price() {
return net_price;
}
public void setNet_price(int net_price) {
this.net_price = net_price;
}
// calculating discount provided by college
public void discount() {
discount = (int) ((sticker_price - net_price) / sticker_price * 100);
}
// returning the discount
public int getDiscount() {
return discount;
}
}
public class MainClass {
public static void main(String[] args) {
// static members created to hold values which should not change with new object
// creation
int avg_stick_price = 0;// to hold average sticker price
int avg_net_price = 0;// to hold average net price
int avg_discount = 0;// to hold the discount
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of colleges to be entered: ");
int n = sc.nextInt();// asking user to provide length of array
CollegeInfo[] college_array = new CollegeInfo[n];// creating college array
for (int i = 0; i < n; i++)// running loop till n
{
college_array[i] = new CollegeInfo();// assigning array term class
System.out.println("Enter Name of College: ");
college_array[i].setCollege_name(sc.next());// using setters to enter values
System.out.println("Enter Sticker Price: ");
college_array[i].setSticker_price(sc.nextInt());
avg_stick_price = avg_stick_price + college_array[i].getSticker_price();// using getters to calculate
// total for calculating average
System.out.println("Enter Net Price: ");
college_array[i].setNet_price(sc.nextInt());
avg_net_price = avg_net_price + college_array[i].getNet_price();
college_array[i].discount();
avg_discount = avg_discount + college_array[i].getDiscount();
}
System.out.format("%50s %16s %16s %16s ", "College", "Sticker Price", "Net Price", "% Discount Rate");
avg_stick_price = avg_stick_price / n;
avg_net_price = avg_net_price / n;
avg_discount = avg_discount / n;
// printing the result
for (int j = 0; j < n; j++) {
if (college_array[j].getDiscount() >= 60) {
// using string formatting
System.out.format("%50s %16s %16s %16s ", college_array[j].getCollege_name() + "**",
"$" + college_array[j].getSticker_price(), "$" + college_array[j].getNet_price(),
college_array[j].getDiscount());
} else if (college_array[j].getDiscount() >= 50) {
System.out.format("%50s %16s %16s %16s ", college_array[j].getCollege_name() + "*",
"$" + college_array[j].getSticker_price(), "$" + college_array[j].getNet_price(),
college_array[j].getDiscount());
} else {
System.out.format("%50s %16s %16s %16s ", college_array[j].getCollege_name(),
"$" + college_array[j].getSticker_price(), "$" + college_array[j].getNet_price(),
college_array[j].getDiscount());
}
}
System.out.format("%50s %16s %16s %16s", "Average", "$" + avg_stick_price, "$" + avg_net_price, avg_discount);
}
}
Run as:
javac MainClass.java
java MainClass