Results 1 to 6 of 6
  1. #1
    sonikku is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Get integer value from constant string

    Java Code:
    public class riding {
    	
    	static final int CLEAR = 3;
    
    	public riding() {
    		
    	}
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		String test = "CLEAR";
    		
    		System.out.println(CLEAR);
    
    	}
    
    }
    How would I derive the constant value of CLEAR which is 3 from a string that contains the constant name. "CLEAR" in this case.

    Basically a string contains a constant name, how do I retrieve the value of said constant?
    Last edited by sonikku; 05-16-2013 at 07:54 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,292
    Blog Entries
    7
    Rep Power
    24

    Default Re: Get integer value from constant string

    The short answer is: reflection, but a longer answer/question is: why do you want to do this? Have you considered a Map<String, Integer> that maps names (Strings) to their values (Integers)?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Get integer value from constant string

    I agree use a map or a enum depending on what you are using it for.

  4. #4
    sonikku is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Re: Get integer value from constant string

    Quote Originally Posted by JosAH View Post
    The short answer is: reflection, but a longer answer/question is: why do you want to do this? Have you considered a Map<String, Integer> that maps names (Strings) to their values (Integers)?

    kind regards,

    Jos
    Project for one of my classes, making a simple CPU emulator/

    Basically I have a bunch on static int's that are supposed to be opcodes. I'm supposed to be able to read a file with certain commands(e.g. BNZ, BZ, ADD, SUB) and execute them. Say for example in the file I read "ADD". I need to be able to convert that to the corresponding Opcode and pass it on to the stack.

    That's why I asks this.

    CLEAR is an Opcode in my program and I need a way to get the numerical value of "CLEAR" to pass on to my "stack".

  5. #5
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: Get integer value from constant string

    I would definitely use a Map<String, Integer> in that case.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,292
    Blog Entries
    7
    Rep Power
    24

    Default Re: Get integer value from constant string

    Quote Originally Posted by sonikku View Post
    Project for one of my classes, making a simple CPU emulator/

    Basically I have a bunch on static int's that are supposed to be opcodes. I'm supposed to be able to read a file with certain commands(e.g. BNZ, BZ, ADD, SUB) and execute them. Say for example in the file I read "ADD". I need to be able to convert that to the corresponding Opcode and pass it on to the stack.

    That's why I asks this.

    CLEAR is an Opcode in my program and I need a way to get the numerical value of "CLEAR" to pass on to my "stack".
    Use a Map<String, Opcode> where the key is the textual representation of the opcode and the value is the Opcode itself; I mention the Opcode class because you can be sure that you need more than just a simple int value for the opcode; e.g. it can contain a Runnable that executes the opcode.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. How to convert integer to String
    By mCoder in forum New To Java
    Replies: 2
    Last Post: 12-27-2011, 05:00 AM
  2. String to Integer Conversion in JSP
    By vinikz in forum New To Java
    Replies: 8
    Last Post: 11-10-2010, 03:45 PM
  3. check if String is an integer?
    By McChill in forum New To Java
    Replies: 5
    Last Post: 05-02-2009, 07:51 PM
  4. Integer to String
    By zervine in forum Forum Lobby
    Replies: 3
    Last Post: 09-12-2008, 12:07 PM
  5. String to Integer conversion
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 04:59 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
  •