Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Error on switch statement

  1. #1
    Junior Member
    Join Date
    Jul 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error on switch statement

    I have the following code components in my source file. There are all in one class, in the same source file. First, the declaration of my enum type:
    class Lexer {
     
    protected enum NumStates {  // Number-lexer states
    		S0, // start state
    		SIGN, // sign has been seen at start
    		INTEGER, // seen a digit
    		BAREINTEGER, // seen an integer without sign or decimal point
    		HEXINTEGER,  // while scanning an integer
    		DECIMAL, // decimal point seen after integer
    		BAREDECIMAL, // decimal point seen with no preceding digit
                    ...more of the same, boring

    Then the method that uses it:

       protected Lexeme ScanNumber(InputFileManager source) throws LexerException, InputFileManager.InputFileManagerException, IOException
        { 
         NumStates state = NumStates.S0;  // <=== THIS IS AN 'enum' VARIABLE!!!
         String num = "";
         while(true)
            { /* scan number */
             char ch = source.GetCharacter();
             switch(state)

    For the switch statement, I get the error
    Cannot switch on a value of type Lexer.NumStates. Only convertible int values, strings or enum variables are permitted	Lexer.java	/Parser/src	line 493	Java Problem

    Of the hundreds of errors I was getting, after I downloaded Eclipse, and "jre-8u131-macosx-x64.dmg", I managed to fix several hundred with a simple reconfiguration. After far too much time spent with Google Search, I found an explanation for my many errors. At the declaration of the enum, it told me that I could not use 'enum' as a variable name, and it was all downhill from there. Upping the conformance to 1.7 was required to solve this. More searching. I found yet more advice, so I followed it and went into the preferences, and modified the preferences for this project:
    Screen Shot 2017-07-11 at 9.26.28 PM.jpg
    This project compiled and ran under Eclipse Neon under Windows; I am using Eclipse Kepler under Mac OS X, which I just installed today. For some reason, when I clicked on the icon to download Orion, I got Kepler instead. If this problem can be solved by downloading Orion, please tell me how to get it (I tried three times and kept getting Kepler, so I gave up). My Windows machine is in the Computer Hospital, where it was taken by ambulance, and I'm told it may need an organ transplant. I have work to do, so I moved to my MacBook.

    Here's most of the information about Eclipse. It looked like many of the plugins (the ones truncated at the bottom of the display) are not relevant to what I am doing. Perhaps none of these are relevant, either, but the small "About" box at the top left of the screenshot may say everything that is important.
    Screen Shot 2017-07-11 at 9.49.03 PM.jpg

    ****I AM NOT AN ECLIPSE OR JAVA ENVIRONMENT EXPERT! Please do not give me explanations that assume that I have a clue about what I am doing. The Eclipse documentation does not even hint that I have to have installed a particular version of JRE to support Eclipse. This is discovered when the program fails to start. And involves more searching for "Why does Eclipse fail to start" articles. So I need simple instructions that even a retired PhD can understand. In the past, I installed Eclipse, and it "just ran". I am a Java programmer, not an Eclipse Build Environment/JRE expert.****

    Of course, pointers to good articles are a great help. After hours of searching I was unable to locate anything about this particular error that was relevant (lots of articles about using strings, no help there).

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Error on switch statement

    If this is a question about how to use Eclipse, you'll have to wait for an expert on that topic. I don't use Eclipse.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error on switch statement

    Quote Originally Posted by Norm View Post
    If this is a question about how to use Eclipse, you'll have to wait for an expert on that topic. I don't use Eclipse.
    No, it is primarily a question of "why does Java give me a compilation error here?" I indicate that I am using Eclipse so that the context is understood.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Error on switch statement

    why does Java give me a compilation error
    Ok, I don't use eclipse and don't have a problem using an enum in switch statements with the javac program, so I don't know what your problem is.
    You use eclipse and have a problem so I assume the problem is with eclipse.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to turn my If statement into a case/switch statement?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: June 19th, 2014, 03:11 PM
  2. can i do this with switch statement?
    By m7abraham in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2014, 02:58 PM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  4. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM

Tags for this Thread