Java Programming Question

Working on a hw assignment and I am getting this following error. Please help me understand what is occurring. Thank you!

Sandwich.java:5: error: illegal start of expression
public String MainIngredient;
^
Sandwich.java:6: error: illegal start of expression
public String Bread;
^
Sandwich.java:7: error: illegal start of expression
public double Price;}
^
3 errors
Error: Could not find or load main class Sandwich

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Sandwich 
{
    public static void main(String[] args)
{
    private String MainIngredient;
    private String Bread;
    private double Price;}
    public String getMainIngredient()
    {
        return MainIngredient;
    }
    public void setMainIngredient(String mainIngredient)
    {
        MainIngredient = mainIngredient;
    }
    public String getBread()
    {
        return Bread;
    }
    public void setBread(String bread)
    {
        Bread = bread;
    }
    public double getPrice()
    {
        return Price;
    }
    public void setPrice(double price)
    {
        Price = price;
    }
}
Dude this is a c++ only site, try repl.it or something. Stack Overflow. We either can’t or won’t help you, sry.
Reminder: this is a C++ forum. Java questions are best taken to a Java forum, and are off-topic here.

That said, the issue you're dealing with is pretty universal: you're declaring your fields inside main(). Try moving lines 5 to 7 (excluding the closing brace) outside of main() and your issue should clear right up.

-Albatross
not a java expert (you have come to a forum for a totally different language. Its like asking the French language experts to help you do your Russian homework).

But it looks to me like the private members of the class should not be in the function, but outside it up in the class level scope. That is it looks like you have mangled the class and the main method together, and they should be more distinct. Why the heck main is inside a class anyway... freakin java...
Last edited on
from your error message public String MainIngredient;
from your code private String MainIngredient;

they don't match, so the errors are not referring to that code
Topic archived. No new replies allowed.