[Logo]
Forums Register Login
Illegal start of type; identifier expected problem.
Hi all,
I'm having a problem with my java program. I am trying to make a program where the user enters a product number and a quantity, then at the end of the program the total of the order pops up. I have gotten as far as the last statement on my code without compilation errors, but when I try to display the total it either won't show up (if I move it up a bracket) or I get a compilation error. Can someone possibly help me with this? Thanks!



The error I get is this:

----jGRASP exec: javac -g C:\jGRASP Projects\JavaLoopsandSwitchStatements.java

JavaLoopsandSwitchStatements.java:65: <identifier> expected
JOptionPane.showMessageDialog(null, "The total is: " + orderAmount);
^
JavaLoopsandSwitchStatements.java:65: illegal start of type
JOptionPane.showMessageDialog(null, "The total is: " + orderAmount);
^
JavaLoopsandSwitchStatements.java:65: illegal start of type
JOptionPane.showMessageDialog(null, "The total is: " + orderAmount);
^
JavaLoopsandSwitchStatements.java:65: ')' expected
JOptionPane.showMessageDialog(null, "The total is: " + orderAmount);
^
JavaLoopsandSwitchStatements.java:65: ';' expected
JOptionPane.showMessageDialog(null, "The total is: " + orderAmount);
^
JavaLoopsandSwitchStatements.java:65: illegal start of type
JOptionPane.showMessageDialog(null, "The total is: " + orderAmount);
^
JavaLoopsandSwitchStatements.java:65: <identifier> expected
JOptionPane.showMessageDialog(null, "The total is: " + orderAmount);
^
JavaLoopsandSwitchStatements.java:65: ';' expected
JOptionPane.showMessageDialog(null, "The total is: " + orderAmount);
^
JavaLoopsandSwitchStatements.java:66: reached end of file while parsing
}
(I apologize if I didn't format the errors correctly.)

Thanks, Kat
Hi,


the error causing because the line
"JOptionPane.showMessageDialog(null, "The total is: " + orderAmount);"

is in Class not in method main.

Please enter this in main method,
this would solve your problem.
Thank you so much for your help. The last time I moved it up a line, I forgot to press "-1" to exit, so I thought it wasn't working.

Thanks!!
 

Kathleen Tillman wrote:I may sound like a dunce, but I'm not quite sure what you mean by "in class not in method main".
Does that mean I need to move it farther down or farther up? And wouldn't that screw up showing the total after the information has been entered? I'm really new to Java, so this may sound like a really dumb question, but I'm confused.
Thanks.



Last line of your code is out of the main method...
Welcome to JavaRanch

We have all had that sort of error ourselves. All lines of code have locations they can be put in, and (usually) places they can't. That line is a statement, so it has to be inside a method (or initialiser block, or constructor). In this case, it fits a method. You will also notice that you have got about 9 messages, all caused by the same error. You will get used to that soon enough.

This thread has been viewed 8251 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 15, 2018 08:42:53.