aspose file tools*
The moose likes Beginning Java and the fly likes for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop" Watch "for loop" New topic
Author

for loop

chris x barrett
Greenhorn

Joined: Dec 20, 2005
Posts: 10
my compiler won't compile this for loop

for (int cell : locationCells)

it says that the colon should be a semicolon... i am using a book to do this program and that is what it calls for and i know there are for loops as such

for (int i = 0; i < 10; i++)

i can't figure out what the problem it ��??
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

what version of the JVM do you have? type:

java -version

the enhance for-loop was put into 1.5, so if you're not running that, i can see why it wouldn't work.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
chris x barrett
Greenhorn

Joined: Dec 20, 2005
Posts: 10
i checked that too to make sure and i have java 1.5
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

can you post the relevant code and the EXACT error message your getting? please use the code tags around your code, to preserve formatting. just use the handy "Instant UBB Code" buttons below to get the tags, then paste your code inside...

errr... the buttons are below when you are posting your reply, not when you're just viewing the thread, if i didn't make that clear
[ December 20, 2005: Message edited by: fred rosenberger ]
chris x barrett
Greenhorn

Joined: Dec 20, 2005
Posts: 10
This is the class that gives me the error
errors are

';' expected on line 17
illegal start of expression on line 31
illegal start of expression on line 32


1public class SimpleDotCom
2{
3int[] locationCells;
4int numOfHits = 0;
5
6public void setLocationCells(int[] locs)
7{
8locationCells = locs;
9}
10
11public String checkYourself(String stringGuess)
12{
13int guess = Integer.parseInt(stringGuess);
14String result = "miss";
15//int cell = 0;
16for (int cell : locationCells) // i = 0; i < 7; i ++)
17{
18if (guess == cell)
19{
20result = "hit";
21numOfHits++;
22//cell++;
23break;
24}
25}
26if (numOfHits == locationCells.length)
27{
28result = "kill";
29}
30System.out.println(result);
31return result;
32}
33}
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Execute:
javac -J-version
Ensure that you are not passing the -source or -target switch to a 1.5 level compiler.


Tony Morris
Java Q&A (FAQ, Trivia)
Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134
It compiles without an error for me. When I execute the command java -version on my console I get this

java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)


"Failure is not when you fall down; its only when you fail to get up again."
chris x barrett
Greenhorn

Joined: Dec 20, 2005
Posts: 10
ok i figured out the problem .. it was the compiler that i was using wasn't using java 5. it was using an older version so when i tryed it in bluej it compiles and runs. before i was using Jcreator and i don't know how to (or if you can) change the version of java it compiles with. But original problem fixed
chris x barrett
Greenhorn

Joined: Dec 20, 2005
Posts: 10
ok now i am saposed to upgrade the code to ask for a userinput as well and use ArrayList to make my life easyer but again i get a compile problem not with the extended for loop anymore though now the error is



[ December 20, 2005: Message edited by: chris barrett ]
[ December 20, 2005: Message edited by: chris barrett ]
chris x barrett
Greenhorn

Joined: Dec 20, 2005
Posts: 10
that isn't saposed to be a smile. i didn't use the code button.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by chris barrett:
that isn't saposed to be a smile. i didn't use the code button.

When you are composing a post, there is a checkbox near the bottom of the page to "Disable smilies in this post." (Otherwise, a semicolon followed by a closing parenthesis will display as a winking smilie.)

You can go back and edit your own posts by clicking on the paper/pencil icon.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
chris x barrett
Greenhorn

Joined: Dec 20, 2005
Posts: 10
thank you

i am still can't find/figure out why i keep getting that error though.
[ December 20, 2005: Message edited by: chris barrett ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The error message...
setLocationCells(java.util.ArrayList<java.lang.String>) in DotCom cannot be applied to (int[])

...is telling you that your method setLocationCells is expecting an ArrayList (of Strings), but you are trying to call it using an int array instead.
[ December 20, 2005: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop