Results 1 to 8 of 8
  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Read column from Excel represented as Enum

    Hello,

    Assuming that we have Excel file and the elements of requested column represented as Enum class
    Java Code:
    public enum FederalState {BW, BY, BE, BB, BR, HH, HE, MV, NI, NRW, RP, SL, SN, ST, SH, TH}
    As know in Enum there are no setters and getters so how to represent our data from enum class to read that column which contain the elements.

    Java Code:
    //Iterate through each rows from first sheet
    	    Iterator<Row> rowIterator = sheet.iterator();
    	    while(rowIterator.hasNext()) {
    	        Row row = rowIterator.next();
    	        
    	        //display from the first row 
    	        if(row.getRowNum() > 0)
    	        {
    	        	FederalState[] loc = FederalState.values();
    // do some thing to read the elements from that column
    That all what i could do it , i doubt if FederalState[] loc = FederalState.values(); is correct and if correct i have no idea how to continue using object "loc" as long as i don't have any setters or getters. (or should i create another class to represent the data with setters and getters?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default Re: Read column from Excel represented as Enum

    Quote Originally Posted by vector_ever View Post
    As know in Enum there are no setters and getters
    Not so.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Read column from Excel represented as Enum

    Not so
    What not so? can i create Setters nad getters from
    Java Code:
    public enum FederalState {BW, BY, BE, BB, BR, HH, HE, MV, NI, NRW, RP, SL, SN, ST, SH, TH}
    if yes then how?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default Re: Read column from Excel represented as Enum

    An enum can contain fields and methods too, you know. Even a main(...) method.
    Java Code:
    public enum MyEnum {
    
      MY_ENUM_1, MY_ENUM_2, MY_ENUM_3;
      private int value;
    
      public int getValue() {
        return value;
      }
    
      public void setValue(int value) {
        this.value = value;
      }
    
      public static void main(String[] args) {
        MY_ENUM_1.setValue(3);
        MY_ENUM_2.setValue(5);
        MY_ENUM_3.setValue(10);
    
        System.out.println(MY_ENUM_1.getValue());
        System.out.println(MY_ENUM_2.getValue());
        System.out.println(MY_ENUM_3.getValue());
      }
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Read column from Excel represented as Enum

    ok, i know it, but still not regular Seeters and getters, and stay the main question how to use the enum in another class to represent the data of Enum class
    Java Code:
    public enum FederalState {BW, BY, BE, BB, BR, HH, HE, MV, NI, NRW, RP, SL, SN, ST, SH, TH;
    
    	private String value;
    	
    	public String getValue() {
    	  return value;
    	}
    	
    	public void setValue(String value) {
    	  this.value = value;
    		}
    	
    	private FederalState(){	
    	}
    
    	private FederalState(String value) {
    		this.value = value;
    	}
    		}
    Java Code:
    //Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
            while(rowIterator.hasNext()) {
                Row row = rowIterator.next();
                 
                //display from the first row 
                if(row.getRowNum() > 0)
                {
                    FederalState[] loc = FederalState.values();
    // do some thing to read the elements from that column
    With normal Class (where i have normal getters and setters) the wanted code will be something like that
    Java Code:
    //Iterate through each rows from first sheet
    	    Iterator<Row> rowIterator = sheet.iterator();
    	    while(rowIterator.hasNext()) {
    	        Row row = rowIterator.next();
    	        
    	        //display from the first row 
    	        if(row.getRowNum() > 0)
    	        {
    	        	DistributionGridOperator transOp = new DistributionGridOperator();
    	        	transOp.setName(row.getCell(2).getStringCellValue());
    	        	transOp.setOperatorId(Double.toString(row.getCell(1).getNumericCellValue()));
    	        	
    	        	set.add(transOp);
    	        }
    how to get the same result using Enum?
    Last edited by vector_ever; 08-11-2013 at 05:21 PM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default Re: Read column from Excel represented as Enum

    Quote Originally Posted by vector_ever View Post
    ok, i know it, but still not regular Seeters and getters
    I don't see what's irregular there ...

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Read column from Excel represented as Enum

    I don't meant it like that, but i meant how to use this getters and setters
    Java Code:
    public enum FederalState {BW, BY, BE, BB, BR, HH, HE, MV, NI, NRW, RP, SL, SN, ST, SH, TH;
    
    	private FederalState value;
    	
    	public FederalState getValue() {
    	  return value;
    	}
    	
    	public void setValue(FederalState value) {
    	  this.value = value;
    		}
    	
    	private FederalState(){
    		
    	}
    
    	private FederalState(FederalState value) {
    		this.value = value;
    	}
    		}
    The main Problem i can't create object from my class (enum in this case) to use it to get our data throw getters and setters, exactly like this:
    Java Code:
    //display from the first row 
                if(row.getRowNum() > 0)
                {
                    DistributionGridOperator transOp = new DistributionGridOperator();
                    transOp.setName(row.getCell(2).getStringCellValue());
                    transOp.setOperatorId(Double.toString(row.getCell(1).getNumericCellValue()));
                     
                    set.add(transOp);
                }

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: Read column from Excel represented as Enum

    All enums have an implicit valueOf() method that accepts a String and returns the enum that matches that String, or throws an exception if the String is not valid.

    So if you have a column that contains those state abbreviations then you can simply pass that to the valueOf method and get the enum back.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 05-29-2013, 09:36 AM
  2. How to read from excel?
    By ginger0001 in forum Apache POI
    Replies: 2
    Last Post: 04-02-2012, 10:50 AM
  3. Column width in excel through jsp
    By syed0071 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-05-2011, 04:16 PM
  4. Replies: 2
    Last Post: 06-08-2010, 01:21 PM
  5. Excel Column
    By Gajesh Tripathi in forum New To Java
    Replies: 2
    Last Post: 04-13-2008, 02:26 PM

Posting Permissions

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