• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constant Expression Required  RSS feed

 
Pacha Mia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me what's wrong with the following code. I get the error "constant expression required" at the case statement but the following is from my book example. Thanks!
import java.io.*;

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch! I added code tags to your post to make it a bit easier to read.

Changing st.TT_WORD to StreamTokenizer.TT_WORD (and similarly for the other case) fixes the compilation error. I think this is a case of the compiler not being smart enough to realize the two code snippets are equivalent in meaning.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the issue is that the variables in a case must be compile-time constants.
 
Pacha Mia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!