• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

for loop

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ��??
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
chris x barrett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i checked that too to make sure and i have java 1.5
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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}
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Execute:
javac -J-version
Ensure that you are not passing the -source or -target switch to a 1.5 level compiler.
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

 
chris x barrett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that isn't saposed to be a smile. i didn't use the code button.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
chris x barrett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic