Results 1 to 3 of 3

Thread: Why won't this compile?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved Why won't this compile?

    Compiler keeps complaining about <identifier> expected at 1st in the enum GearState.
    Code:
    class Transmission{
      public enum GearState{neutral,1st,2nd,3rd,4th,5th,6th};
      private int currentgear = GearState.neutral;
    
      public void setGear(GearState gs){
        currentgear = gs;
      }
      public GearState getNextGear(){
      switch(currentgear){
       case neutral: return GearState.neutral;
       case 1st: return GearState.1st;
       case 2nd: return GearState.2nd;
       case 3th: return GearState.3rd;
       case 4th: return GearState.4th;
       case 5th: return GearState.5th;
       case 6th return GearState.6th;
      }
     }
      public void printGears(){
       for(GearState gs: GearState.values()){
        System.out.println(gs);
       }
      }
     }
     public class Car {
      public static void main(String[] args){
       Transmission trans = new Transmission();
       System.out.println(trans.getNextGear());
       trans.setGear(Transmission.GearState.1st);
       System.out.println(trans.getNextGear());
      }
     }

  2. #2
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    Re: Why won't this compile?

    Code:
    class Transmission{
        public enum GearState{neutral,first,second,third,fourth,fifth}; // first letter of a variable shouldn't be a digit
        private GearState currentgear=GearState.neutral; // type mismatch if we use a non GearState identifier
    
        public void setGear(GearState gs){
            currentgear=gs;
        }
        public GearState getNextGear(){
            return currentgear; // why don't we just return the current gear state?
        }
        public void printGears(){
            for(GearState gs:GearState.values()){
                System.out.println(gs);
            }
        }
    }
    public class test{
        public static void main(String[] args){
            Transmission t=new Transmission();
            System.out.println(t.getNextGear());
            t.setGear(Transmission.GearState.first);
            System.out.println(t.getNextGear());
        }
    }

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Why won't this compile?

    Opps i forgot that they are identifers. With the getNextGear() method i asked myself the same thing. I was going by an article from a november issue of jdj volume: 9 issue :11.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width