• Post Reply Bookmark Topic Watch Topic
  • New Topic

random number generator and local variables  RSS feed

 
Laura Tobin
Greenhorn
Posts: 18
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am still blasting through the Easy Steps book and so far so good, but this one has me frustrated a bit. I kind of feel like I know what's wrong but not what the fix would look like.

Basically this code is supposed to create an int array of 50 elements, then pull the elements listed in positions 1-6 to make a lottery draw.

Problem is it gets down to that last "For" statement and then says "duplicate local variable i." If I attempt to separate it from the previous for loop with curly braces, then it gets mad and says it doesn't know what "local variable i" even IS. So I tried changing it to "j" in the last statement and it couldn't recognise that either. I feel like I need to change the variable name in that second for loop but I'm not sure how to make it understand that the second variable is going to be outputting the values from the first variable.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing you need to do is format that code better:


Next...look VERY CAREFULLY ad your for loop on line 19. Where do you think it ends? I'll give you a hint...it is NOT line 24. It is much sooner than that. You may want to fix that before you try doing anything else...
 
Laura Tobin
Greenhorn
Posts: 18
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaah! Yes! Thank you!!

I killed the extra semicolon, it was still not compiling so I put the closing brace up on the first for loop and wham! Success!

I swear, I need a color-coded editor....
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you need a colour‑coded editor. I tell people that sort of thing all the time. Look in this post (links in first line). You also need to learn to write backwards (last but one link in that post), because you can get all sorts of errors from unmatched {}.
Also always use {} after if, while, etc. Get into the habit of writing if...)newlineOrSpace{...}. That way you will have the braces there ready to accept the body of the statement, and you will resist the temptation to put a semicolon in there.

And don't mix the indentation where you put { on a line by itself with the convention where { is appended to the line.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I worry when I see lots of casts. There are, I think, easier ways to create “random” numbers, which you will find discussed in this thread.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You also need to learn to write backwards, because you can get all sorts of errors from unmatched {}.
Also always use {} after if, while, etc.

I don't think of it as writing backwards...I think of it as "write the entire if-statement before i write anything else." Since I never write more than 2-3 lines at a time before compiling, the most I'd ever write would be something like:


Once I compile and test THAT, then I'd start filling in the body with whatever needs to happen. Same thing with a for loop. My first iteration would never be much more than:


That way I know my loop works before I clutter it up with other lines of code...
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Laura Tobin wrote:I swear, I need a color-coded editor....

You might find this list of editors helpful.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're on Windows, I vote for Notepad++. In a Unix-like environment, good old vim will work.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Notepad++ is very good.
 
Jake Augustine
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell and Fred,
I just wanted to say thanks for the great advice. Thinking about it now it seems kind of obvious in order to avoid silly syntax errors, but I had never even thought about doing that. As Campbell wrote in the thread he referenced, I was typing code like I was typing a letter. I would make so many Compiler errors due to stupid, little mistakes ( like when I learned about @Override and responded with @override ). Glad I joined this forum since I've started learning Java. Been a great reference.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gee shucks!! Yer got us all emibarrassed!

But, pleased to be able to help
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I call it writing backwards because I move backwards from the } to the body of the block. Write { then enter then } then ↑ then enter then tab with automatic conversion to 4 spaces. The ↑ takes me backwards.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I get the moving backwards from the '}' part. But to me, it's "finish writing the entire if-statement before writing anything else". Heck...I usually even do the same thing with the parens around the condition. My first pass is usually this:


That is the foundation for my if-statement. I'm always going to have at least that, and it's easy to see it all being there.

But ultimately, it's a tomato/tomato thing (boy, that doesn't work at ALL when it's written out, does it?)
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct pronunciation is of course tomato.

I am afraid you have defeated me there, Fred. You have cleverly shown how you can go backwards twice per statement. I think we are talking about the same thing but calling it something different.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!