• Post Reply Bookmark Topic Watch Topic
  • New Topic

Syntax error on token(s), misplaced construct(s)  RSS feed

 
Wesley Grove
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting the above-mentioned error on lines 25 and 34, and I'm not sure why. Any help would be appreciated. Thanks in advance for everyone who responds

If you guys spot any other errors, feel free to point them out, but I should be able to take care of them more easily, I just don't understand this one.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ; ends a statement.  There shouldn't be a ; after a while or if statement. For example:
 
Wesley Grove
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing that out, fixed it up, I also removed my for loop in case 66 as I realized that there was never a scenario in which I would use it, just to let anyone who might respond know.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You actually have quite a few other syntax errors in your code. Perhaps, it would help if you showed us the error messages from your compiler?

Henry
 
Wesley Grove
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
You actually have quite a few other syntax errors in your code. Perhaps, it would help if you showed us the error messages from your compiler?

Henry

sure, I'll throw them up real quick, I've also made a few changes so I'll reupload the source code as well.


File: C:\Users\wesley.grove\Desktop\Programs\DiceRoller.java  [line: 25]
Error: Syntax error on token(s), misplaced construct(s)
File: C:\Users\wesley.grove\Desktop\Programs\DiceRoller.java  [line: 34]
Error: Syntax error on token "case", assert expected
File: C:\Users\wesley.grove\Desktop\Programs\DiceRoller.java  [line: 35]
Error: Syntax error on tokens, delete these tokens
File: C:\Users\wesley.grove\Desktop\Programs\DiceRoller.java  [line: 41]
Error: Syntax error, insert ")" to complete Expression
File: C:\Users\wesley.grove\Desktop\Programs\DiceRoller.java  [line: 41]
Error: Syntax error on token ")", delete this token
File: C:\Users\wesley.grove\Desktop\Programs\DiceRoller.java  [line: 47]
Error: Syntax error on token "case", assert expected
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure those error messages go with the posted code?
How are you compiling the code to get those messages?
 
Wesley Grove
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes those are the errors for that code, but I can see where you're confused, there seems to be a small formatting error on the code window, the lines and line numbers don't quite line up so it looks like line 33 is line 34.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you compiling the code to get those messages?
I don't get those errors with the javac.exe compiler.
 
Wesley Grove
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using the Eclipse compiler through an IDE
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the javac.exe command and see what you get.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To use the javac program, you should read parts of this wiki.  However, if you want to continue using Eclipse, that's okay.  Notice that you will get red exes in the left margin when there is a syntax error.  Always correct these right away.  Don't let a bunch of them pile up.  This is because the error can cause subsequent errors.  Sometimes when you have many errors, just fixing the first will help.

The first line to look at is Switch(diceType). Remember that Java is case sensitive.  Use the lowercase "switch" and see what errors you have after that.  There are a lot of things wrong with your program, but let's start there.

 
Wesley Grove
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:To use the javac program, you should read parts of this wiki.  However, if you want to continue using Eclipse, that's okay.  Notice that you will get red exes in the left margin when there is a syntax error.  Always correct these right away.  Don't let a bunch of them pile up.  This is because the error can cause subsequent errors.  Sometimes when you have many errors, just fixing the first will help.

The first line to look at is Switch(diceType). Remember that Java is case sensitive.  Use the lowercase "switch" and see what errors you have after that.  There are a lot of things wrong with your program, but let's start there.

Thanks for all the advice, Switch threw me off because my IDE recognized the capital S version as a class and the two have similar colors, I'm planning to change that now. I sadly cannot use javac because I have to do all of my code for the most part of a school computer so I can't change my path or java_home, nor can I access websites to help me circumvent that. I do however have a second IDE that does provide different error messages when I compile my code through it, but I have no idea what software it is using to compile it. I've fixed all the errors and I can now compile without issue, however I do get a java.lang.NullPointerException when I run it. any advice on that would be great as well. Thanks everyone who pointed out my errors.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
get a java.lang.NullPointerException

Please copy the full text of the error message and paste it here. It has important info about the error.

The error message has the source line number where the exception happened.  Look at that line, find the variable with the null value (print the variables if needed) and then backtrack in the code to see why that variable does not have a valid value.
 
Wesley Grove
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wasn't listing as an error, just an output on the interactions pane. I ran it through my other IDE and figured it out, I forgot to put static in the second line of code. I've got the whole thing working now and I added a few features so it doesn't loop endlessly. Thanks again everyone.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wesley Grove wrote:It wasn't listing as an error, just an output on the interactions pane
You mean it is not a compile‑time error, but an error in the code which only manifests itself at runtime with an Exception.
. . . . I forgot to put static in the second line of code. . . ..
That is most probably adding an error rather than correcting one. Despite the wording of some compile‑time error messages, the solution usually requires the removal of the keyword static. I can't see where you could have written static in your code and still persuaded it to compile.
Also never use == true or == false, which are both poor style and very error‑prone if you write = by mistake,
Never
while (b == true) ...
but
while (b) ...
Never
while (b == false) ...
but
while (!b) ...
The same applies to if etc.
[edit]Add bang sign (!) forgotten in last line of code.
 
Wesley Grove
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the formatting tips, I implemented them into my code. I put static right before void and my code is working perfectly now. What would you advise other than static? I'm still just beginning in Java and so far all of the programs I've made and read through my textbook have that in the second line.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure where you put static.  If it's in the main() method, then it's necessary, but almost anywhere else it's not.  One common mistake of beginning programmers is putting a lot of code in main()Here's a wiki about what to do rather than that.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wesley Grove wrote:. . . What would you advise other than static? . . .
Remove the keyword static from your vocabulary except before the main method. Later on, you will learn what it actually means and you will know when static shou‍ld be used. It is convenient to put code in the main method because it allows you to show a control structure on its own, but that is not correct programming practice in Java®.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!