I can't figure out why I am recieving error messages from the compiler that tells me that I have an illegal start of the expression for the code that is in red. I have also put the product class that goes with the main class; I'm not sure if the product class is a cause of the error or not because I am new to Java. Can someone tell what I am doing wrong and how to fix it?
import java.util.Scanner; //input scanner
public class Inventory
{
// main method begin program execution
public static void main( String args[] )
{
public String productName; // product name
public double number; // product item number
public double units; // number of units in stock
public double price; // price per unit
public double value; // total value of all units in stock
// scanner to obtain user input
Scanner input = new Scanner ( System.in );
// scanner product object
{
Product = new Product(productName, number, units, price);
}
// value total of all units
{
System.out.printf(p.toString() );
}
} // end main method
} // end Inventory class
Here is the product class that goes with the inventory class
public class Product
{
private String productName; //product name
private double number; // item number
private double units; // number of units in stock
private double price; // price per unit
private double value; // total value of all units in stock
//constuctor to initialize productName
public Product( String name, double item_number, double units_stocked, double price_unit, double value_total )
{
productName = name;
number = item_number;
units = units_stocked;
price = price_unit;
} // end constuctor
// method to set product name
public void setProductName( String name )
{
productName = name;
} // end method setProductName
// method to set item number
public void setNumber(int item_number)
{
number = item_number;
} // end method setNumber
// method to set units in stock
public void setUnits(int units_stocked)
{
units = units_stocked;
} // end method setUnits
// method to set price per unit
public void setPrice(int price_unit)
{
price = price_unit;
} // end method setPrice
// method to retrieve product name
public String getProductName()
{
return productName;
} // end method getProductName
// method to retrieve item number
public double getNumber()
{
return number;
} // end method getNumber
// method to retrieve units in stock
public double getUnits()
{
return units;
} // end method getUnits
// method to retrieve price per unit
public double getPrice()
{
return price;
} // end method getPrice
// method to retrieve total value of all units
public double getValue()
{
value = units * price;
return value;
} // end method getValue
} // end product class
Please help.
public static void main( String args[] )
{
public String productName; // product name
public double number; // product item number
public double units; // number of units in stock
public double price; // price per unit
public double value; // total value of all units in stock
You use the access specifiers (public,private, protected) only for Class level variables, not for local variables, I suggest you please get a good book and clear out your basics of Java.
Though Stephen has already pointed this out, you need to get rid of the access modifiers when declaring data types in your main method.
The reason is because it is irrelevant for a method to have access specifiers within its scope when you are declaring local variables that will not "escape" the scope of the method unless returned, and even so it would be a copy of that value (or a copy of a pointer) in which you will have access to it publicly.
Also consider the following--
import java.util.Scanner; //input scanner
public class Inventory001
{
// main method begin program execution
public static void main( String args[] )
{
String productName; // product name
double number; // product item number
double units; // number of units in stock
double price; // price per unit
double value; // total value of all units in stock
// scanner to obtain user input
Scanner input = new Scanner ( System.in );
Product001 p = null;
// scanner product object
{
// You need to specify 5 values for your constructor!
//p = new Product001(productName, number, units, price);
}
// value total of all units
{
System.out.printf(p.toString() );
}
} // end main method
} // end Inventory class
I attempted to fix your constructor problem, but I want you to think of a few reasons of why it is incorrect so that you wont make the same mistake again.
I will work on it some more to see if I can get it right this time and better understand what I need to do and why. Java has proven to be extremely hard for me. Thank you Alex for your explainations and suggestions. Someone giving me explainations is a bigger help than telling me to just read about it because my text (that I have read over and over) confuses me sometimes. Thank you.
I will work on it some more to see if I can get it right this time and better understand what I need to do and why. Java has proven to be extremely hard for me. Thank you Alex for your explainations and suggestions. Someone giving me explainations is a bigger help than telling me to just read about it because my text (that I have read over and over) confuses me sometimes. Thank you.
no offence, but the title of your thread asks us to help you understand the error messages your compiler give you. first part of that would be, indeed, to read those error messages.
the first post contains some code, of which indeed the main method doesn't make to much sense, but then again, trying is the first step to success. mostly the error messages an exception give you state the reason why your code doesn't work and the line of code where the problem occurs, so although you don't like me say so: don't just read the error message, try to understand it. if you have any problems like this in the future, and you want us to help you understand your error message, it might be a good idea to provide those error messages.
no offence, but the title of your thread asks us to help you understand the error messages your compiler give you. first part of that would be, indeed, to read those error messages.
the first post contains some code, of which indeed the main method doesn't make to much sense, but then again, trying is the first step to success. mostly the error messages an exception give you state the reason why your code doesn't work and the line of code where the problem occurs, so although you don't like me say so: don't just read the error message, try to understand it. if you have any problems like this in the future, and you want us to help you understand your error message, it might be a good idea to provide those error messages.
I appologize for not being more clear on my compiler errors. The only error messages that I had was an illegal start of expression for each line that I highlighted in red in the code that I posted. I will post them differently next time so that there is no confusion. I understood what the error meant when it told me that it was illegal start of expression but was unsure of what was illegal about it.
I have no problem with what you say or try to tell me, I am just having extreme difficulties understanding the basics of Java even with all the reading and research that I have done. I might as well be reading chinese.