I have been beating my head against the walls about the all time favorite Cars Inventory program. I got Cars to compile but won't run due to a exception in thread "main"java.lang.NoSuchMethodError:main
and spent many hours....trying to eliminate error.

// Cars.java
//Car inventory program

public class Cars
{
private int itemNumber; // item number
private String itemName; // item name
private int itemTotal; // total of cars in stock
private double itemPrice; // price per car
private double totalInventory; // total of cars in inventory

// initialize constructor with four arguments
public Cars( int number, String name, int total, double price )
{

itemNumber = number;
itemName = name;
itemTotal = total;
itemPrice = price;

} // end of constructor with four arguments

public void setItemNumber( int number )
{
itemNumber = number;
}

public int getItemNumber()
{
return itemNumber;
}

public void setItemName( String name )
{
itemName = name;
}
public String getitemName()
{

return itemName;
}
public void setItemTotal( int total )
{
itemTotal = ( total = 9 );
}
public int getItemTotal()
{
return itemTotal;
}
public void setItemPrice (double price)
{
itemPrice = ( price = 26738.0);
}
public double getItemPrice()
{
return itemPrice;
}
public double getValue()
{
return itemTotal * itemPrice;
}

}// end class Cars

Cars Inventory2 has 3 errors which I can not eliminate either.
Line 29 has an invalid method declaration; return type required.
gettotalInventory();
and a 'class" or 'interface' expected in line 45

public class CarsInventory2
{               
         public static void main( String args[ ] )
         { 
                  // create and initialize Cars array
                  Cars[ ] myCarArray = new Cars [7];
         
                  myCarArray[0] = new Cars( 2479, "Volkswagon CC", 20, 33000.00 );
                  myCarArray[1] = new Cars( 4862, "Ford Fusion", 10, 25000.00 );
                  myCarArray[2] = new Cars( 12945,"Subaru Legacy", 5, 21000.00 );
                  myCarArray[3] = new Cars( 46789, "Lincoln MKS", 4, 42000.00 );
                  myCarArray[4] = new Cars( 76042, "Chevy Cobalt", 7, 18000.00 );
                  myCarArray[5] = new Cars( 9210, "Honda Civic", 2, 23000.00 );
                  myCarArray[6] = new Cars( 4269, "Kia Optima", 12, 21000.00 );            
                  }
                  public double gettotalInventory() ;
                  {
                           return totalInventory;
                  }

                  public double getProdInventory() ;
                  {
                           return ProdInventory;
                  }

                  // Output value for each array element
                 
	 gettotalInventory();
                  {
                           for ( int i = 0; i < myCarArray.length; i++ )
                  {
                           System.out.println( "Product Number" + myCarArray[ i ]. getProdNumber() );
                           System.out.println( "Product Name" + myCarArray[ i ]. getProdName() );                 
                           System.out.println( "Total Units in Stock" + myCarArray[ i ]. getUnitsTotal() );
                           System.out.println( "Price Per Unit:" + myCarArray[ i ]. getUnitPrice() );
                           System.out.println( "Amount of Product Inventory:" + myCarArray[ i ]. getProdInv() );
                           System.out.println( "Total Inventory" + myCarArray[ i ]. getTotalInventory() );
                  }

                 

         } // end main

} // end CarsInventory2

The third bit of code is CarsInventory which is linked to my Cars code some how....aarrgghh!!! I am so lost it hurts my brain. I am gettin a compile error of ')' expected System.out.printf("%s %s\n", item getItemName() );

public class CarsInventory
 {
	public static void main( String args[]) 
{

		// main method begins execution Car application
		Cars item = new Cars(47036, " Toyota Civic", 9, 26738);

		// get car details
	System.out.println(" Car Inventory Program");
	System.out.printf(" %s %d\n", " Item Number: ", item.getItemNumber() );
	System.out.printf(" %s %s\n", " Item Name: ", item getItemName() );
	System.out.printf(" %s %d\n", " Total items in stock: ", item.getItemTotal() );
	System.out.printf(" %s $%g\n", " Price per item: ", item.getItemPrice() );

	}// end main method

I have spent more than a dozen hours trying to eliminate errors.
Please help so I can get through this class with a decent grade.....
I am falling behind in my other class being obsessed with these problems.

Thanx for your assistance.

jim

1. Class Cars doesn't have a main method, so how do you'd expect it to run ? If you need to run it put a main method in it which acts as an entry point for a program to run.

2. In the CarsInventory2 class

public double gettotalInventory() ; // remove the semi-colon here
                  {
                           return totalInventory;
                  }
 
                  public double getProdInventory() ; // remove the semi-colon here
                  {
                           return ProdInventory;
                  }

then again

{ // remove this opening brace, thats why you get the class or interface req. error
                           for ( int i = 0; i < myCarArray.length; i++ )
                  {

3. In the CarsInventory class you do not have the closing brace for the class, you have put a closing brace for main thats it.

Verruct24,

Thanx for the reply, I am new to Java...for some reason it is not sticking in my head. I really want to learn this for future use.
I changed Cars and added the main method and now have 14 errors, mostly "illegal start of expression". Do I have too many {}'s? It indicates an upward arrow under the "p" in private in lines 8,9,10,11,12,15.
Plus have identifier arrow under = on lines 18,19,20,21 and class or interface under v in void setItemNumber( int number ) in line 25

// Cars.java
//Car inventory program

public class Cars
{
  	public static void main( String args[ ] )
{
private int itemNumber; // item number
private String itemName; // item name
private int itemTotal; // total of cars in stock
private double itemPrice; // price per car
private double totalInventory; // total of cars in inventory

// initialize constructor with four arguments
public Cars( int number, String name, int total, double price )
}

itemNumber = number;
itemName = name;
itemTotal = total;
itemPrice = price;

} // end of constructor with four arguments

public void setItemNumber( int number )
{
itemNumber = number;
}

public int getItemNumber()
{
return itemNumber;
}

public void setItemName( String name )
{
itemName = name;
}
public String getitemName()
{

return itemName;
}
public void setItemTotal( int total )
{
itemTotal = ( total = 9 );
}
public int getItemTotal()
{
return itemTotal;
}
public void setItemPrice (double price)
{
itemPrice = ( price = 26738.0);
}
public double getItemPrice()
{
return itemPrice;
}
public double getValue()
{
return itemTotal * itemPrice;

         } // end main

} // end class Cars

I made the changes like you said in CarsInventory2 and now have on line 28 invalid method declaration; return type required gettotalInventory();
line 30 illegal start of type for ( int i = 0; i <my CarArrary.length; i++)
line 39 <identifier> expected
line 44 class or identifier expected } //end CarsInventory2 with arrow under }
line 44 class or identifier expected } //end CarsInventory2 with arrow

after CarsInventory2^
I sometimes get confused when I fix one thing and 5 more pop up...

Thats because you have enveloped all your methods in the main, you cannot have the declaration/definition of a method inside a method.

You have put all your method definitions inside one method (main) whereas they should be outside of it. So you put the closing curly brace for main immediately after the main method ends and hence remove the methods from inside the main method.

Edited 6 Years Ago by verruckt24: n/a

This article has been dead for over six months. Start a new discussion instead.