8 Replies - 508 Views - Last Post: 01 February 2010 - 09:07 PM Rate Topic: -----

#1 TracyLB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 10

Error message: illegall start of expression

Posted 31 January 2010 - 11:07 PM

I am trying to sort an array and I keep getting this error: illegal start of expression public Inventory[] SortInventory(Inventory[]DVDs) and I don't understand why... If you can point out where I've gone wrong I would greatly appreciate it.

Here is my code:
//Inventory Program Part 1
//Inventory Class - product class that holds the item number, the name of the product, the number of
//units in stock, and the price of each unit.


public class Inventory
{

 private double ItemNumber; //to store Item number
 private String ItemName; //to store Item name
 private double UnitsInStock; //to store number of units in stock
 private double UnitPrice; //to store Unit price
 //private double InventoryValue; //to store Inventory value
 //private double TotalInventory; //to store Total Inventory value
 //private String theInventory; //to store the Inventory

 public Inventory() //constructor with no initial variables

   {
	 this ("", 0.0, 0.0, 0.0);
   }

   public Inventory (String name, double number, double stock, double price)

	{
	  ItemName = name; //set Item name
	  ItemNumber = number; //set Item number
	  UnitsInStock = stock; //set Units in stock
	  UnitPrice = price; //set Unit price
	}

	 //method to set the Item name
	 public void setItemName( String name )

	 {
	   ItemName = name; //store the Item name
	 } //end method setItemName

	 //method to get the Item name
	 public String getItemName()

	 {
	   return ItemName;
	 } //end method getItemName

	 //method to set the Item number
	 public void setItemNumber( int number )

	 {
	   ItemNumber = number; //store the Item number
	 } //end method setItemNumber

	 //method to get the Item number
	 public double getItemNumber()

	 {
	   return ItemNumber;
	 } //end method getItemNumber

	 //method to set the UnitsInStock
	 public void setUnitsInStock( double stock )

	 {
	   UnitsInStock = stock; //store the Units in stock
	 } //end method setUnitsInStock

	 //method to get the UnitsInStock
	 public double getUnitsInStock()

	 {
	   return UnitsInStock;
	 } //end getUnitsInStock

	 //method to set the UntiPrice
	 public void setUnitPrice( double price )

	 {
	   UnitPrice = price; //store the Unit price
	 } //end method setUnitPrice

	 //method to get the UnitPrice
	 public double getUnitPrice()

	 {
	   return UnitPrice;
	 } //end getUnitPrice

	 //method to set Inventory value
	 //public void setInventoryValue(double value)

	 //{
	 //  InventoryValue = value;
	 //} //end method setInventoryValue

	 //method to get InventoryValue
	 //public double getInventoryValue()

	 //{
	 //  return InventoryValue;
	 //} //end method to getInventoryValue

	 public double getInventoryValue()

	 {
	   return UnitPrice*UnitsInStock;
	 } //end method to getInventoryValue

	 //method to set TotalInventory
	 //public void setTotalInventory(double value)

	 //{
	 //TotalInventory = total;
	 //} //end method setTotalInventory

	 //method to get TotalInventory
	 //public double getTotalInventory()

	 //{
	 //return TotalInventory;
	 //} //end method to getTotalInventory




} //end class Inventory


//Inventory Test program part 1
//InventoryTest class

import java.util.Arrays;


public class InventoryTest1

{
	public static void main( String args[] )

	{

		// instantiate an Inventory object
		Inventory myInventory = new Inventory();

		//displays welcome message
		  System.out.println ("DVD Collection Inventory");
			System.out.println();//skips a line




		//create and initialize an array of DVDs
		Inventory[] DVDs = new Inventory[5];

		DVDs[0] = new Inventory("The Day After Tomorrow", 112.1, 7, 12.99);
		DVDs[1] = new Inventory("Dejavu", 114.1, 25, 14.99);
		DVDs[2] = new Inventory("Live Free or Die Hard", 116.1, 15, 10.99);
		DVDs[3] = new Inventory("Fantastic Four", 102.1, 10, 0.99);
		DVDs[4] = new Inventory("The Island", 104.1, 5, 9.99);

		//sort through the array
		public Inventory[] SortInventory(Inventory[]DVDs)
		{
			Inventory tmp;
			for (int index = 0; index < DVDs.length; index++)
			{
				for(int anotherindex = index + 1; anotherindex < DVDs.length; anotherindex++
				{
					String s1 = DVDs[index].getItemName();
					String s2 = DVDs[anotherindex].getItemName();
					If(s1.compareTo(s2)>0)
					{
						tmp = DVDs[index];
						DVDs[index] = DVDs[anotherindex];
						DVDs[anotherindex] = tmp;
					}
				}
			}

			return DVDs;

		}


		//For each array element, output value
		for (int count = 0; count < DVDs.length; count++ )
		{

	   		System.out.printf("Product Name:  %s\n", DVDs[count].getItemName());
			System.out.printf("Item Number:  %4.2f\n", DVDs[count].getItemNumber());
			System.out.printf("Units In Stock:  %.2f\n", DVDs[count].getUnitsInStock());
			System.out.printf("Unit Price: $%4.2f\n", DVDs[count].getUnitPrice());
			System.out.printf("Inventory Value:  $%4.2f\n", DVDs[count].getInventoryValue());
			System.out.println(); //blank line to seperate products
		}



	}//end main

}//end class InventoryTest1




Is This A Good Question/Topic? 0
  • +

Replies To: Error message: illegall start of expression

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Error message: illegall start of expression

Posted 31 January 2010 - 11:19 PM

Simple error to make. You are creating your method within the main method, this isnt allowed. Also, wont work having two seperate classes like that, because you havnt made it an inner class, and anyway, the inner class wouldnt like the static main method. So create a seperate file for each of the classes. This is a fix
import java.util.Arrays;

public class Inventory
{

 private double ItemNumber; //to store Item number
 private String ItemName; //to store Item name
 private double UnitsInStock; //to store number of units in stock
 private double UnitPrice; //to store Unit price
 //private double InventoryValue; //to store Inventory value
 //private double TotalInventory; //to store Total Inventory value
 //private String theInventory; //to store the Inventory

 public Inventory() //constructor with no initial variables

   {
	 this ("", 0.0, 0.0, 0.0);
   }

   public Inventory (String name, double number, double stock, double price)

	{
	  ItemName = name; //set Item name
	  ItemNumber = number; //set Item number
	  UnitsInStock = stock; //set Units in stock
	  UnitPrice = price; //set Unit price
	}

	 //method to set the Item name
	 public void setItemName( String name )

	 {
	   ItemName = name; //store the Item name
	 } //end method setItemName

	 //method to get the Item name
	 public String getItemName()

	 {
	   return ItemName;
	 } //end method getItemName

	 //method to set the Item number
	 public void setItemNumber( int number )

	 {
	   ItemNumber = number; //store the Item number
	 } //end method setItemNumber

	 //method to get the Item number
	 public double getItemNumber()

	 {
	   return ItemNumber;
	 } //end method getItemNumber

	 //method to set the UnitsInStock
	 public void setUnitsInStock( double stock )

	 {
	   UnitsInStock = stock; //store the Units in stock
	 } //end method setUnitsInStock

	 //method to get the UnitsInStock
	 public double getUnitsInStock()

	 {
	   return UnitsInStock;
	 } //end getUnitsInStock

	 //method to set the UntiPrice
	 public void setUnitPrice( double price )

	 {
	   UnitPrice = price; //store the Unit price
	 } //end method setUnitPrice

	 //method to get the UnitPrice
	 public double getUnitPrice()

	 {
	   return UnitPrice;
	 } //end getUnitPrice

	 //method to set Inventory value
	 //public void setInventoryValue(double value)

	 //{
	 //  InventoryValue = value;
	 //} //end method setInventoryValue

	 //method to get InventoryValue
	 //public double getInventoryValue()

	 //{
	 //  return InventoryValue;
	 //} //end method to getInventoryValue

	 public double getInventoryValue()

	 {
	   return UnitPrice*UnitsInStock;
	 } //end method to getInventoryValue

	 //method to set TotalInventory
	 //public void setTotalInventory(double value)

	 //{
	 //TotalInventory = total;
	 //} //end method setTotalInventory

	 //method to get TotalInventory
	 //public double getTotalInventory()

	 //{
	 //return TotalInventory;
	 //} //end method to getTotalInventory




}//end class Inventory


//Inventory Test program part 1
//InventoryTest class






public class InventoryTest1

{
	public static void main( String args[] )

	{

		// instantiate an Inventory object
		Inventory myInventory = new Inventory();

		//displays welcome message
		  System.out.println ("DVD Collection Inventory");
			System.out.println();//skips a line




		//create and initialize an array of DVDs
		Inventory[] DVDs = new Inventory[5];

		DVDs[0] = new Inventory("The Day After Tomorrow", 112.1, 7, 12.99);
		DVDs[1] = new Inventory("Dejavu", 114.1, 25, 14.99);
		DVDs[2] = new Inventory("Live Free or Die Hard", 116.1, 15, 10.99);
		DVDs[3] = new Inventory("Fantastic Four", 102.1, 10, 0.99);
		DVDs[4] = new Inventory("The Island", 104.1, 5, 9.99);

		//sort through the array
		


		//For each array element, output value
		for (int count = 0; count < DVDs.length; count++ )
		{

			   System.out.printf("Product Name:  %s\n", DVDs[count].getItemName());
			System.out.printf("Item Number:  %4.2f\n", DVDs[count].getItemNumber());
			System.out.printf("Units In Stock:  %.2f\n", DVDs[count].getUnitsInStock());
			System.out.printf("Unit Price: $%4.2f\n", DVDs[count].getUnitPrice());
			System.out.printf("Inventory Value:  $%4.2f\n", DVDs[count].getInventoryValue());
			System.out.println(); //blank line to seperate products
		}



	}//end main
	
	public Inventory[] SortInventory(Inventory[]DVDs)
	{
		Inventory tmp;
		for (int index = 0; index < DVDs.length; index++)
		{
			for(int anotherindex = index + 1; anotherindex < DVDs.length; anotherindex++)
			{
				String s1 = DVDs[index].getItemName();
				String s2 = DVDs[anotherindex].getItemName();
				if(s1.compareTo(s2)>0)
				{
					tmp = DVDs[index];
					DVDs[index] = DVDs[anotherindex];
					DVDs[anotherindex] = tmp;
				}
			}
		}

		return DVDs;

	}
}//end class InventoryTest1


Was This Post Helpful? 0
  • +
  • -

#3 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: Error message: illegall start of expression

Posted 31 January 2010 - 11:21 PM

Put a space inbetween inventory[] and dvds. Also, I think the problem may lie with the fact that your method is defined inside the main() method.

**edit** nick2price beat me to it :)

This post has been edited by 333OnlyHalfEvil: 31 January 2010 - 11:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 Qball522  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 86
  • Joined: 06-October 08

Re: Error message: illegall start of expression

Posted 31 January 2010 - 11:24 PM

You had a few syntax errors in your code.

First thing you have a method defined inside of the main method.
public static void main( String args[] )

	{

		// instantiate an Inventory object
		Inventory myInventory = new Inventory();

		//displays welcome message
		  System.out.println ("DVD Collection Inventory");
			System.out.println();//skips a line




		//create and initialize an array of DVDs
		Inventory[] DVDs = new Inventory[5];

		DVDs[0] = new Inventory("The Day After Tomorrow", 112.1, 7, 12.99);
		DVDs[1] = new Inventory("Dejavu", 114.1, 25, 14.99);
		DVDs[2] = new Inventory("Live Free or Die Hard", 116.1, 15, 10.99);
		DVDs[3] = new Inventory("Fantastic Four", 102.1, 10, 0.99);
		DVDs[4] = new Inventory("The Island", 104.1, 5, 9.99);

		//sort through the array

//Right here, move this outside of the main method
		public Inventory[] SortInventory(Inventory[]DVDs)
		{
			Inventory tmp;
			for (int index = 0; index < DVDs.length; index++)
			{
				for(int anotherindex = index + 1; anotherindex < DVDs.length; anotherindex++
				{
					String s1 = DVDs[index].getItemName();
					String s2 = DVDs[anotherindex].getItemName();

//Change the "If" to "if"
					If(s1.compareTo(s2)>0)
					{
						tmp = DVDs[index];
						DVDs[index] = DVDs[anotherindex];
						DVDs[anotherindex] = tmp;
					}
				}
			}

			return DVDs;

		}



Compiled and ran fine for me after those few changes.
That Illegal start of expression was telling you that you had another method inside your main method.
Was This Post Helpful? 0
  • +
  • -

#5 TracyLB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 10

Re: Error message: illegall start of expression

Posted 31 January 2010 - 11:41 PM

Okay it compiled and ran, but it did not sort.

Here is the corrected code:


//Inventory Test program part 1
//InventoryTest class

import java.util.Arrays;


public class InventoryTest1

{
	public static void main( String args[] )

	{

		// instantiate an Inventory object
		Inventory myInventory = new Inventory();

		//displays welcome message
		  System.out.println ("DVD Collection Inventory");
			System.out.println();//skips a line



		//create and initialize an array of DVDs
		Inventory[] DVDs = new Inventory[5];

		DVDs[0] = new Inventory("The Day After Tomorrow", 112.1, 7, 12.99);
		DVDs[1] = new Inventory("Dejavu", 114.1, 25, 14.99);
		DVDs[2] = new Inventory("Live Free or Die Hard", 116.1, 15, 10.99);
		DVDs[3] = new Inventory("Fantastic Four", 102.1, 10, 0.99);
		DVDs[4] = new Inventory("The Island", 104.1, 5, 9.99);

		//For each array element, output value
		for (int count = 0; count < DVDs.length; count++ )
		{

	   		System.out.printf("Product Name:  %s\n", DVDs[count].getItemName());
			System.out.printf("Item Number:  %4.2f\n", DVDs[count].getItemNumber());
			System.out.printf("Units In Stock:  %.2f\n", DVDs[count].getUnitsInStock());
			System.out.printf("Unit Price: $%4.2f\n", DVDs[count].getUnitPrice());
			System.out.printf("Inventory Value:  $%4.2f\n", DVDs[count].getInventoryValue());
			System.out.println(); //blank line to seperate products
		}



	}//end main

	public Inventory[] SortInventory(Inventory[] DVDs)
			{
				Inventory tmp;
				for (int index = 0; index < DVDs.length; index++)
				{
					for(int anotherindex = index + 1; anotherindex < DVDs.length; anotherindex++)
					{
						String s1 = DVDs[index].getItemName();
						String s2 = DVDs[anotherindex].getItemName();

						if(s1.compareTo(s2)>0)

						{
							tmp = DVDs[index];
							DVDs[index] = DVDs[anotherindex];
							DVDs[anotherindex] = tmp;
						}
					}
				}

				return DVDs;

			}


}//end class InventoryTest1



Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Error message: illegall start of expression

Posted 01 February 2010 - 12:15 AM

here is one problrm i noticed:
index < DVDs.length



you should iterate the first loop until length - 1 index.

also, the code wont sort your array since you dont call your sort method in your code.
Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 726
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Error message: illegall start of expression

Posted 01 February 2010 - 12:44 AM

looks like bubble sort
for (int index = 0; index < DVDs.length-1; index++)
{
	for(int anotherindex = 0; anotherindex < DVDs.length-index-1;anotherindex++)
	{
		String s1 = DVDs[anotherindex].getItemName();
		String s2 = DVDs[anotherindex + 1].getItemName();
		if(s1.compareTo(s2)>0)
		{
			tmp = DVDs[index];
			DVDs[index] = DVDs[index+1];
			DVDs[index+1] = tmp;
		}
	}
}



also you need to call the sort method in your main

EDIT: edited to indent the code

This post has been edited by mostyfriedman: 01 February 2010 - 12:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 TracyLB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 10

Re: Error message: illegall start of expression

Posted 01 February 2010 - 08:56 PM

I added the code to call the sort method but it is still not working. Now I get the error ';' expected after the public static void main...

I am so confused!

Here is my code:
//Inventory Program Part 1
//Inventory Class - product class that holds the item number, the name of the product, the number of
//units in stock, and the price of each unit.


public class Inventory
{

 private double ItemNumber; //to store Item number
 private String ItemName; //to store Item name
 private double UnitsInStock; //to store number of units in stock
 private double UnitPrice; //to store Unit price
 //private double InventoryValue; //to store Inventory value
 //private double TotalInventory; //to store Total Inventory value
 //private String theInventory; //to store the Inventory

 public Inventory() //constructor with no initial variables

   {
	 this ("", 0.0, 0.0, 0.0);
   }

   public Inventory (String name, double number, double stock, double price)

	{
	  ItemName = name; //set Item name
	  ItemNumber = number; //set Item number
	  UnitsInStock = stock; //set Units in stock
	  UnitPrice = price; //set Unit price
	}

	 //method to set the Item name
	 public void setItemName( String name )

	 {
	   ItemName = name; //store the Item name
	 } //end method setItemName

	 //method to get the Item name
	 public String getItemName()

	 {
	   return ItemName;
	 } //end method getItemName

	 //method to set the Item number
	 public void setItemNumber( int number )

	 {
	   ItemNumber = number; //store the Item number
	 } //end method setItemNumber

	 //method to get the Item number
	 public double getItemNumber()

	 {
	   return ItemNumber;
	 } //end method getItemNumber

	 //method to set the UnitsInStock
	 public void setUnitsInStock( double stock )

	 {
	   UnitsInStock = stock; //store the Units in stock
	 } //end method setUnitsInStock

	 //method to get the UnitsInStock
	 public double getUnitsInStock()

	 {
	   return UnitsInStock;
	 } //end getUnitsInStock

	 //method to set the UntiPrice
	 public void setUnitPrice( double price )

	 {
	   UnitPrice = price; //store the Unit price
	 } //end method setUnitPrice

	 //method to get the UnitPrice
	 public double getUnitPrice()

	 {
	   return UnitPrice;
	 } //end getUnitPrice

	 //method to set Inventory value
	 //public void setInventoryValue(double value)

	 //{
	 //  InventoryValue = value;
	 //} //end method setInventoryValue

	 //method to get InventoryValue
	 //public double getInventoryValue()

	 //{
	 //  return InventoryValue;
	 //} //end method to getInventoryValue

	 public double getInventoryValue()

	 {
	   return UnitPrice*UnitsInStock;
	 } //end method to getInventoryValue

	 //method to set TotalInventory
	 //public void setTotalInventory(double value)

	 //{
	 //TotalInventory = total;
	 //} //end method setTotalInventory

	 //method to get TotalInventory
	 //public double getTotalInventory()

	 //{
	 //return TotalInventory;
	 //} //end method to getTotalInventory
	 
	 public void sortInventory()




} //end class Inventory


//Inventory Test program part 1
//InventoryTest class

import java.util.Arrays;


public class InventoryTest1

{
	 public static void main( String args[] )

	 public static void sort(Inventory DVDs[])
		 {

	// instantiate an Inventory object
	Inventory myInventory = new Inventory();

	//displays welcome message
	  System.out.println ("DVD Collection Inventory");
		System.out.println();//skips a line



	//create and initialize an array of DVDs
	Inventory[] DVDs = new Inventory[5];

	DVDs[0] = new Inventory("The Day After Tomorrow", 112.1, 7, 12.99);
	DVDs[1] = new Inventory("Dejavu", 114.1, 25, 14.99);
	DVDs[2] = new Inventory("Live Free or Die Hard", 116.1, 15, 10.99);
	DVDs[3] = new Inventory("Fantastic Four", 102.1, 10, 0.99);
	DVDs[4] = new Inventory("The Island", 104.1, 5, 9.99);


	//For each array element, output value
				for (int count = 0; count < DVDs.length; count++ )
				{

	   		 System.out.printf("Product Name:  %s\n", DVDs[count].getItemName());
		 System.out.printf("Item Number:  %4.2f\n", DVDs[count].getItemNumber());
		 System.out.printf("Units In Stock:  %.2f\n", DVDs[count].getUnitsInStock());
		 System.out.printf("Unit Price: $%4.2f\n", DVDs[count].getUnitPrice());
		 System.out.printf("Inventory Value:  $%4.2f\n", DVDs[count].getInventoryValue());
			  System.out.println(); //blank line to seperate products
	}



	 }//end main

	 public Inventory[] SortInventory(Inventory[] DVDs)
	{
   		 Inventory tmp;
		 for (int index = 0; index < DVDs.length; index++)
		 {
				for(int anotherindex = index-1; anotherindex < DVDs.length; anotherindex++)
			  {
			 String s1 = DVDs[index].getItemName();
			 String s2 = DVDs[anotherindex].getItemName();

				  if(s1.compareTo(s2)>0)

				  {
			tmp = DVDs[index];
			DVDs[index] = DVDs[anotherindex];
			DVDs[anotherindex] = tmp;
				  }
			   }
		}

		  return DVDs;

				}


}//end class InventoryTest1



Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,611
  • Joined: 27-December 08

Re: Error message: illegall start of expression

Posted 01 February 2010 - 09:07 PM

The reason you have semi-colon expected errors at these two lines public void sortInventory() and public static void main( String args[] ) is b/c you have two method headers without braces to define them. If you add
braces like so: public static void main( String args[] ){} for both of them, the errors will go away, but the two methods won't do anything.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1