Can anyone tell me where I am going wrong with my code? I keep getting two errors stating ';' expected (on lines 22 & 23). I am new to this so I am not sure what I can post and what I can't, so I am just going to post my code and you can tell me different. Thank you.

// 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; // amount of cars in inventory

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

      itemNumber = number;
      itemName = name;
      setItemTotal = total of cars;
      setItemPrice = price per car;  

      } // 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



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

      //main method begins execution of java Car application
      Cars item = new Cars(
         47036, "  Toyota Camry"  , 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:  "  ,
            product.get.ItemName()  );
         System.out.printf( "  %s  %d\n"  ,  "  Total items in stock:  "  ,
            product.getItemsTotal()  );
         System.out.printf( "  %s $%g\n"  , "  Price per item:  "  ,
            product.getItemPrice() );

   }// end main method

}// end class Cars

Edited 3 Years Ago by Nick Evan: Fixed formatting

try

itemTotal = total;
itemPrice = price;

instead of

setItemTotal = total of cars;
setItemPrice = price per car;

Edited 6 Years Ago by thomas_naveen: typo

I tried it and got 14 errors so I changed it back. Thank you though.

Look at my edited post. Make that change and the following changes as well.

several issues that I failed to spot at first are:

public void setItemTotal(int total);

No semicolon is needed at the end of this line.

Same problem exists at other places as well.

private double totalInventory;

is not being used anywhere in the program.

Also , please use code tags when you post


[EDIT]

One thing more is that you should not be using two 'Cars' classes. Change one to CarDriver or something.

The two classes should be in two files and the file name should correspond to the class name.

Better still use a package for both these classes.

Edited 6 Years Ago by thomas_naveen: n/a

Okay, so I removed the total inventory line and removed the semicolon but I am still gettting the same two errors for the same two lines. Also, what do you mean when you say to use code tags?

You can't use spaces. If you want to name your variable total of cars you simply cannot do that, so use total_of_cars instead. So yes... using invalid syntax is a sure way to cause errors.

Also, the number of errors you get does not necessarily matter. In other words, you could be getting 15 errors, change something, then get one... but that does not mean that your code is better or that you are on the right track. If you pay attention to your errors and fix the earliest errors first, you will be doing things right, even if after you make the fix, it says there are more errors. You need to read the errors, understand what they say (and if you don't, look them up on google or ask about what the error means here). Then fix them and if more show up, you'll still be comfortable with knowing that what you did was correct, and the error you just fixed was hiding more serious errors. Think of it as a good thing.

Edited 6 Years Ago by BestJewSinceJC: n/a

this is error free code check it..

and product class you need to import .....

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; // amount 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



public static void main (String args[])
{

//main method begins execution of java Car application
Cars item = new Cars(
47036, " Toyota Camry" , 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: " ,
product.get.ItemName() );
System.out.printf( " %s %d\n" , " Total items in stock: " ,
product.getItemsTotal() );
System.out.printf( " %s $%g\n" , " Price per item: " ,
product.getItemPrice() );

}// end main method
}

this is error free code check it..

and product class you need to import .....

public void setItemTotal(int total)
{
itemTotal = ( total = 9 );
}

define "error free".

@ Rmj001: are you trying to assign a boolean value to an integer, or did I miss something?

Edited 6 Years Ago by stultuske: n/a

BestJew, thank you for your advice. I guess I needed to hear that if more errors come up, it's not the end of the world.

thomas_naveen & musthafa.aj : I tried implementing the changes you guys gave me but now I have even more errors and I feel like I am back at step one.

This is what I have now. Any ideas? And in case I forgot-thank you so much for all of your help. This is the 3rd time I take this class and I have never had so much trouble learning something.

// 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;
      setItemTotal = total;
      setItemPrice = 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



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

      //main method begins execution of java Car application
      Cars item = new Cars(
         47036, "  Toyota Camry"  , 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:  "  ,
            product.get.ItemName()  );
         System.out.printf( "  %s  %d\n"  ,  "  Total items in stock:  "  ,
            product.getItemsTotal()  );
         System.out.printf( "  %s $%g\n"  , "  Price per item:  "  ,
            product.getItemPrice() );

   }// end main method

}// end class Cars

The errors I am getting are:

Cars.java:68: duplicate class: Cars
public class Cars
       ^
Cars.java:35: cannot find symbol
symbol  : class string
location: class Cars
   public void setItemName(string name);
                           ^
Cars.java:22: cannot find symbol
symbol  : variable setItemTotal
location: class Cars
      setItemTotal = total;
      ^
Cars.java:23: cannot find symbol
symbol  : variable setItemPrice
location: class Cars
      setItemPrice = price;
      ^
Cars.java:31: missing method body, or declare abstract
   public int getItemNumber();
              ^
Cars.java:33: return outside method
      return itemNumber;
      ^
Cars.java:35: missing method body, or declare abstract
   public void setItemName(string name);
               ^
Cars.java:37: cannot find symbol
symbol  : variable name
location: class Cars
      itemName = name;
                 ^
8 errors

c:\Projects>

Edited 3 Years Ago by Nick Evan: Fixed formatting

Avoid the semicolon after function signature in the definition part.

public void setItemName(string name);

Change

setItemTotal = total;
setItemPrice = price;

to

itemTotal = total;
itemPrice = price;
string

should be replaced with

String

Remove the second

public class Cars

and the braces for it.

Thomas, i tried what you said and most of it worked except removing the second public class cars. so i put it back and now i get the following error:

Cars.java:67: class, interface, or enum expected
Public class Cars
^
1 error

The following is the modified code:

// 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;
      setItemTotal = total;
      setItemPrice = 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


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

      //main method begins execution of java Car application
      Cars item = new Cars(
         47036, "  Toyota Camry"  , 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:  "  ,
            product.get.ItemName()  );
         System.out.printf( "  %s  %d\n"  ,  "  Total items in stock:  "  ,
            product.getItemsTotal()  );
         System.out.printf( "  %s $%g\n"  , "  Price per item:  "  ,
            product.getItemPrice() );

   }// end main method

}//end class Cars

Any ideas?

Edited 3 Years Ago by Nick Evan: Fixed formatting

@rmj......first you should take note of the java rules for writing a program.
Some basic rules are--
1) class name and file name must be same.
2) NO two class shall be declared in the same file name.
and there are other rules also which you should read from a good introductory book.

Now here is a solution for the problem you facing. I have just removed the errors for you.
When u run these programs just remember to keep both the file in the same package or folder.

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); // Bad practice..should not hard code the value
	}

	public int getItemTotal() {
		return itemTotal;
	}

	public void setItemPrice(double price) {
		itemPrice = (price = 26738.0); // Same bad practice here also
	}

	public double getItemPrice() {
		return itemPrice;
	}

	public double getValue() {
		return itemTotal * itemPrice;
	}

}// end class Cars
public class TestCars {
	public static void main(String args[]) {

		// main method begins execution of java Car application
		Cars item = new Cars(47036, " Toyota Camry", 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

}//

Hope this solved your problem....

so does that mean that I don't do both on the same page, I have to do another page with the second public class? Sorry for all the stupid questions.

You can only have one class in each .java file, and the classname must be the same as whatever is before the .java. Does that answer your question?

yes, thank you so much- you are a lifesaver. Thank you to all of you. I could not have done this assignment without you.

This question has already been answered. Start a new discussion instead.