• Post Reply Bookmark Topic Watch Topic
  • New Topic

Syntax errors on token(s), misplaced construct(s) and does not want to accept the loop  RSS feed

 
marius sommerfeld
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi please help me,
i don not understand why the compiler does not want to accept the loop


sorry i dont know how to quote the code properly (new to the forum)
 
marius sommerfeld
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry guys i should describe the code it shall count the average number of trials when rolling a dice till the first 6 of n trials.
the problem is that it does dot accept the for loop when im trying to write the rolls till the dice shows 6 of different trys in an array .
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66263
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

It would be helpful for you to update your code to use proper indentation. Code is surprisingly hard to read when it is not properly indented.

Also please include the exact text of the compiler errors you are getting.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You would appear to have used the code tags correctly, but the code isn't formatted correctly. It also isn't correct to make everything static as you have done. It is usually better to use a Random instance than to try arithmetic on Math#random.

The reason you can't get it to compile is that you have declared the same variable twice in lines 8 and 9. You also don't want the cast in line 11. Which program is giving you those error message? Is it Eclipse?
 
marius sommerfeld
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your reply, I edited the code to the standards and removed 8th line with the double declaration.
I am forced by my university to use drjava which is using eclipse compiler when i am not wrong.
i did not understood what you ment by "cast" further the error i am receiving is
File: /Users/Sommerfeld/Desktop/programming/RollingDice.java  [line: 7]
Error: Syntax error on token(s), misplaced construct(s)
File: /Users/Sommerfeld/Desktop/programming/RollingDice.java  [line: 11]
Error: Syntax error on token "}", delete this token


and what else can i write instead of static and why is it better to use something different,


sorry im have not programmed for quite some years but i really appreciate the help
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because static moves you out of the realms of objects and Java® is an object language.
You shou‍ld write a class with the data as a field and create an instance of that class. I think you need the array as a field and it might be worth having a Random object as a field, too.

Thank you for correcting the indentation but I suspect some of the line numbers have changed. I presume you corrected the compiler errors?
 
marius sommerfeld
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the explenation i tried to remove the errors but it is still giving them
the ones that i posted were regarding the updated code
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove "(int)" from the start of line 9. I don't know what you think you're doing with that but to us (and probably to the compiler, too) it looks like you're trying to perform a cast. A cast is a way to tell the compiler to treat something as the type you specify inside parentheses, in this case, as an int. You can't use casts on the left side of an assignment statement, it can only be on the right side. This is probably why the compiler error message says "misplaced construct."

Your program has logic errors, too, but we'll leave that to you to figure out once you get it running.
 
Dave Tolls
Ranch Foreman
Posts: 3010
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the current code, along with any errors?

Assuming you haven't fixed them with Campbell's and Junilu's help, of course.
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP: be careful with spelling and consistency in the names you use. Names in Java are case sensitive so RollingDice and Rollingdice are two different names even though they differ only by the case of one letter. Rolldice is a whole 'nother name, too.

Also trinals will not be auto spell-checked as trials, it's up to you to watch out for these kind of differences. These are usually reported by the compiler as "undefined token" or something like that.
 
marius sommerfeld
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much im really sorry with bothering with such stupid mistakes, i will watch out for such stuff more catrefully in the future. so all errors are fixed now thank you very much
sorry for the greenhorn mistakes.
how do i declare this post as solved
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are buttons at the bottom of the page to mark the thread as solved but you might want to run your program a few times to see if it's working the way you expect it to. I already mentioned there are logic errors in your code; you might want to address those before saying all's well that ends well.
 
marius sommerfeld
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i will finish the program till tomorrow and then look wether it works or not.
Thank you for the remark
 
marius sommerfeld
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The programm is working properly now
I am going to add the final code once again and close the thread thank you to all who helped me
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, your throwsTillSix() method will always return the number 6. Is that really what you want it to do? What's the point of all the code in there then?x

(EDIT: Ignore this comment - I misinterpreted the code)
 
Knute Snortum
Sheriff
Posts: 4210
124
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Minor: you have some unnecessary parentheses in your code.
can be and can be
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:So, your throwsTillSix() method will always return the number 6. Is that really what you want it to do? What's the point of all the code in there then?

My mistake -- I guess it returns the number of tries to get a roll of 6. I read the code wrong.
 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, when you're going to execute some statements at least one time, a do-while loop may be more appropriate. Random is also easier to use than Math.random(). The example below uses java.util.concurrent.ThreadLocalRandom, which is a subclass of Random.

Alternatively,

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!