• Post Reply Bookmark Topic Watch Topic
  • New Topic

Illegal start of type errors, among others  RSS feed

 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. I am brand new to coding and to java and I'm trying to complete a project for class. I am to build three constructors, a no-arg, one with 2 args and one with 7 args. I am to place them in a class called BookOrder and call them to my driver program of Proj4.java. I have written what I thought was correct code, but I'm getting errors which aren't even errors. Such as

.\BookOrder.java:459: error: illegal start of type
quantity = quan;
^


My BookOrder class:



I'm having no issues in my driver program, but this BookOrder class is a mess. I'm getting like 91 errors in it just like the error at the top and I can't understand what I've done wrong. Any help would be appreciated at this point. Thanks in advance.
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Steve. Will do.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About your problem,

That error usually occurs because there is a missed or extra bracket, paranthesis, or the like.

There are probably multiple errors here. I think a different problem is the root cause. Can you find the very first error you get and post that?

If the error you posted is the first one - it is hard to tell which line causes the problem - there is a lot of code there and the line the error is reported on doesn't match the codelines in the posted code. Can you tell us which line in the above code that error points at?
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error is the first one and it corresponds with line 180 in the posted code. As an aside, should my constructors come before any other methods in my code or does it matter?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lilith,

Your description of the error didn't quite make sense to me, and I already had a suspicion where the first error was, so I copy-pasted and compiled your code. Here's how the first error is presented to me:



The problem is that plus sign (+); I see others later in the code. It's not necessary to use any symbol like that to continue your code across multiple lines in Java. The compiler is getting horribly confused by it!

With those fixed, there are only a few more errors; for example, you've used "date" as the name for a variable when I think you meant "orderDate", and in a switch statement, you can't say



you have to say:


 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is in the method declaration where you have split it into 2 lines. You don't need need the '+' at the start of the second line and in fact it's illegal.

Edit: Doh too slow.
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Ernest! So I only have to use the "+" in cases of print statements? I didn't know that. Thank you. Now all I have to do is fix the other 15 errors. lol

In the switch statement, is using the "||" symbol legal? I wasn't sure if I could or not and now I'm getting a "bad operand" error.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lilith Durham wrote:Thanks, Ernest! So I only have to use the "+" in cases of print statements? I didn't know that. Thank you. Now all I have to do is fix the other 15 errors. lol

In the switch statement, is using the "||" symbol legal? I wasn't sure if I could or not and now I'm getting a "bad operand" error.


No, it is not legal, instead, you define two case statements letting the first 'fall through' to the other:
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lilith Durham wrote:Thanks, Ernest! So I only have to use the "+" in cases of print statements? I didn't know that. Thank you. Now all I have to do is fix the other 15 errors. lol


In a print statement, don't think of the "+" as continuing the lines -- that's not why it's there. It's assembling all the things you want to print into one String. If you want to print several separate quoted Strings in one <tt>System.out.println()</tt>, then you have to use "+" to add them together into the one argument that the <tt>println()</tt> method wants.


In the switch statement, is using the "||" symbol legal? I wasn't sure if I could or not and now I'm getting a "bad operand" error.


Java does have a "||" ("or") operator, but you can't use it in that context. It can be used in expressions like



but switch statements don't use it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Lilith: I see you started off with about 250 lines of code and a question about a compilation error. I hope you didn't write all that code before trying to compile. As a beginner, you should write at most 3-5 lines of code before compiling, and then add at most 3-5 lines and compile again, and so on. Even experienced developers compile often, or use IDEs that show syntax errors as soon as they're typed.

By compiling (and testing, and debugging) in small pieces, when a problem does occur, you don't have to guess or wade through hundreds of lines of code to find out where it came from.

Now, if you were in fact building that code up in pieces, and it compiled find up until you added the last couple of lines, great! But you still shouldn't try to understand it or fix it in the context of the whole program. Create a separate program that has just enough code to reproduce the problem, and then fiddle around with just that one until you understand the problem and its solution. If you still can't figure it out, posting that small program (the SSCCE(←click)) rather than 200+ lines of unrelated stuff, will make it easier for folks here to help you and easier for you to understand that help.

Good luck! And welcome to the Ranch!
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help everyone. I hope I can return the favor someday. lol
 
Marjorie Gyles
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff, thanks for the heads up. No, I stupidly wrote all of it then the driver then tried to compile them and came up with many, many errors. I'll know better next time.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!