• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Java Round-Up game

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Studying for SCJP 1.5 and was toying with the java Round-Up game here on the site.

The question #154 has
for( i=1; k<5; j++) {};

and asks if this will compile (assuming all variables are declared and initialized). The answer is NO because that semicolon is at the end of the for loop is a sytax violation, but the game indicates true because you can have multiple variables in the construct of a for loop.

Am I off here?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have as many semicolons as you want as a statement. It is the empty statement.
 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, it's the semicolon at the end that fails
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interestingly, this rules round up question has been challenged before.

Henry
 
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 Brian Spindler:
no, it's the semicolon at the end that fails


Can you show us some code that demonstrates what you mean?

The following compiles and runs fine for me...
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above code fails to compile because of Line #1;
Says class or interface required. It occurred because of extra semicolon
I have added after import. It happens only if it is not last import
statement. For the last import statement you can have any number of
semicolons.

Thanks,
[ May 31, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using Eclipse and there is a preference in Eclipse that gives me the option to have a compiler error (a red flag) or a compilier warning (a yellow flag) for extra semicolons.

If you're not using Eclipse, you can probably do the same thing with Java 5.0 and lint.

Kaydell
[ June 01, 2007: Message edited by: Kaydell Leavitt ]
 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm....

I compiled and ran the code from marc and it worked fine. Yesterday before I posted I was lazy and stuck in :



to eclipse to see if I would get an error and low and behold I did, however with further investigation it was an error telling me "Unreachable Code" for the semicolon, not that it was illegal.

it appears I am wrong, Chandras however does not compile.
[ June 01, 2007: Message edited by: Barry Gaunt ]
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In Eclipse, I do get an error on the line of the for-loop. Eclipse suggests removing the semi-colon on that line.

I thought that it was a preference because there are so many preferences for "ignore", "warn", or "error", but I couldn't find a preference that would make the error on this line go away.

I propose that somebody removes the question in the cattle-roundup because it is creating confusion.

If the question is kept in the cattle-roundup, the answer is that the code is an infinite loop. Also, I get a warning that the variable i is not used.

Kaydell
 
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
Consider that the SCJP exam is not concerned with how certain situations might be handled by IDEs. So if Eclipse is generating its own warnings or errors, it's actually Eclipse that could be the source of confusion. This is one reason that IDEs are generally not recommended in preparing for the SCJP.
 
It was the best of times. It was the worst of times. It was a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic