using the value of a string in the program code


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: using the value of a string in the program code

  1. #1
    Join Date
    Feb 2004
    Posts
    2

    using the value of a string in the program code

    I'm fairly new to Java so this may be a stupid question!

    Is it possible to use the value of a string to reference a component in the code? For example:

    int width = 2;
    int height = 4;

    String size = "Wall" +width+ "x" +height;

    this.finishBricks = wall.****;



    where **** is equal to the value of the size string, i.e. Wall2x4

  2. #2
    Join Date
    Feb 2004
    Posts
    11
    Two ways to handle this: Reflections or a HashMap, the latter being the simplist. I recommend using a HashMap since Reflections is a bit complex. To learn more about reflections, check out:
    http://java.sun.com/docs/books/tutor...ect/index.html

    Here's a possible solution that you can try:

    You should place objects in the HashMap first:

    HashMap map = new HashMap();
    Wall wall = new Wall();

    int width = 2;
    int height = 4;

    wall.setWidth(width);
    wall.setHeight(height);
    map.put("Wall"+width+"x"+height);



    //...to retrieve the object you place in the map later


    Wall wall = (Wall)map.get("Wall2x4");


    I'm not too sure if that's what you're asking, but basically, if what you want is to retrieve objects using a string, then you'ld typically use a HashMap.

  3. #3
    Join Date
    Feb 2004
    Posts
    2
    Sorry I dont think I explained it very well!! This is what I'm trying to do.

    Wall is a class holding 3 different arrays. The Robot class has an array called finishBricks. When the buildWall method is run it receives the integers width and height which are used to construct the dimensions string. I want to use this string to then load the appropriate Wall array into the finishBricks array.

    I hope this makes sense! Any help would be greatly appreciated!


    class Robot {

    public double[][] finishBricks;

    public void buildWall(int width, int height) {

    Wall w = new Wall();
    String dimensions = "Wall" +width+ "x" +height;

    this.finishBricks = w.xxxx; // where xxxx is replaced by whatever the String dimensions is
    }
    }


    class Wall {

    public double[][] Wall4x2 = {
    {-30.00,-01.60,27.45},
    {-30.00,-06.75,27.45},
    {-30.00,-13.55,27.45},
    {-30.00,-20.35,27.45},
    {-30.00,-22.00,28.65},
    {-30.00,-16.85,28.65},
    {-30.00,-10.05,28.65},
    {-30.00,-03.25,28.65}
    };

    public double[][] Wall3x3 = {
    {-30.00,-01.60,27.45},
    {-30.00,-06.75,27.45},
    {-30.00,-13.55,27.45},
    {-30.00,-15.20,28.65},
    {-30.00,-10.05,28.65},
    {-30.00,-03.25,28.65},
    {-30.00,-01.60,29.85},
    {-30.00,-06.75,29.85},
    {-30.00,-13.55,29.85},
    };

    public double[][] Wall2x4 = {
    {-30.00,-01.60,27.45},
    {-30.00,-06.75,27.45},
    {-30.00,-08.40,28.65},
    {-30.00,-03.25,28.65},
    {-30.00,-01.60,29.85},
    {-30.00,-06.75,29.85},
    {-30.00,-08.40,31.05},
    {-30.00,-03.25,31.05},
    };
    }

  4. #4
    Join Date
    Feb 2004
    Posts
    11
    Okay, I see now. Well, first off, I recommend not using public variables since it violates incapsulation. The only time you should use public variables is if the they're declared final.

    Here's one of way you can accomplish your task:

    //for buildWall()

    public void buildWall(int width, int height) {

    this.finishedBricks = Wall.getBricks("Wall"+width+"x"+height);

    }

    import java.util.*;

    class Wall {

    private double[][] Wall4x2 = {
    {-30.00,-01.60,27.45},
    {-30.00,-06.75,27.45} //,...and so on and so forth
    };

    //definition of Wall3x3 and Wall2x4 excluded for brevity

    private static HashMap map = new HashMap();

    //static initializer
    static {
    map.put("Wall4x2", Wall4x2);
    map.put("Wall3x3", Wall3x3);
    map.put("Wall2x4", Wall2x4);
    }

    public static double[][] getBricks(String key) {
    return (double[][])map.get(key);
    }

    }

  5. #5
    Join Date
    Feb 2004
    Posts
    3
    Why not just do this.....? What would be the point of using the string? You could break up the easily, and do an if-statement. However I see no way to avoid using logic statements altogether.

    Code:
    class Robot {
    	public double[][] finishBricks;
    
    	public void buildWall(int width, int height)
    	{
    		Wall w = new Wall();
    
    		String dimensions = "Wall" +width+ "x" +height;
    
    		// since your width dimensions are never the same, you can get away
    		// with just checking a single dimension
    		switch (width)
    		{
    			case 4: this.finishBricks = w.Wall4x2; break;
    			case 3: this.finishBricks = w.Wall3x3; break;
    			case 2: this.finishBricks = w.Wall2x4; break;
    		}
    	}
    That being said, it is almost possible to use the Java Reflection API to get a class or objects value, based on the string. However, as far as I know, the Field object which you would get as a result does not have methods for returning an array. I have not used the reflection API enough, to do what you are asking. Maybe somebody else might have an idea if what you describe is possible with the reflection API?

    From your description, I am guessing you are basically asking if you can dynamically build variable names, and access a classes variables via that dynamically built name.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center