• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Substitute String with its Values

Hi,

I have an interface as below:

public interface Constants
{
      public static final int INFO1 = 4;
      public static final int INFO2 = 5;
      public static final int INFO3 = 6;
      public static final int INFO4 = 7;
      public static final int INFO5 = 8;
      public static final int INFO6 = 9;
      public static final int INFO7 = 10;
}

And, Im implementing the interface in my class. Now, I want the below said functionality:

class MyClass implements Constants
{
        public String getInfo (int index)
      {
            String fields[] = .....; // will be getting from some input

            int INDEX = Integer.parseInt ("INFO" + index);
            System.out.println (INDEX + "::" + INDEX);
            return fields [INDEX];
      }
}

I want to return the string in fields [4]. Im sending 4 as input param for index while calling 'getInfo' method.

How can I substitute the value 4 in place of INDEX in the return statement. I want to achieve this using the interface variables since the index will be dynamically passed.

Thanks,
Rameshbabu
0
arjunarajan
Asked:
arjunarajan
  • 9
  • 8
  • 2
1 Solution
 
CEHJCommented:
You would probably be much better off using enum for this
0
 
arjunarajanAuthor Commented:
Hi Cehj,

Can you pls give me a sample code for this to achieve with ENUM?

Thanks,
Rameshbabu
0
 
CEHJCommented:
That would make your method rather easier as:
    public String getInfo (Info index)
      {
	return index.toString();
      }

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
objectsCommented:
0
 
arjunarajanAuthor Commented:
Hi,

Im not getting you exactly. How can I fetch the value in a specified index, say index 3 from the array string 'fields'.

Pls give me a full example.
0
 
objectsCommented:
Using a Map for fields may also be more appropriate, allowing you to look up values via an enum value

0
 
arjunarajanAuthor Commented:
Can anyone give me a simple example with Enum for my requirement.
0
 
CEHJCommented:
Your enum would be something like:
public interface Constants {
    public enum Info {private int index;
 
        Info(int index) {
            this.index = index;
        }
        INFO1(4),
        INFO2(5),
        INFO3(6),
        INFO4(7),
        INFO5(8),
        INFO6(9),
        INFO7(10);
    }
}

Open in new window

0
 
CEHJCommented:
To test it:
public class TestEnum {
    public static void main(String[] args) {	
	String s = TestEnum.getConstant(Constants.Info.INFO1);
	System.out.println(s);
    }
 
    public static String getConstant(Constants.Info index) {
	return index.toString();
    }
}

Open in new window

0
 
arjunarajanAuthor Commented:
Hi CEHJ,

Im telling that I will be getting the index number alone as input. I will then concatenate '1', '2', '3', etc with the string 'INFO'.

As per your code how can I substitute INFO1 in the place of Constants.Info.INFO1.

Here I dont know which INFO im going to read. after concatenating, I will get the INFO1, INFO2, etc and the I need the value of INFO1, INFO2, etc.

Hope this is clear. Pls ask me if this is not clear.
0
 
CEHJCommented:
>>Im telling that I will be getting the index number alone as input. I will then concatenate '1', '2', '3', etc with the string 'INFO'.

But what are you ulimately going to do with "INFO" + index ?
0
 
arjunarajanAuthor Commented:
When I concatenate INFO + index, I will be getting INFO1, INFO2, INFO3, etc...

I then want to get the value of that INFO1, INFO2, INFO3, etc from the declared constants in the interface.

That is, first i construct on string and then want to get the numeric value stored in a variable with constructed string name.

0
 
CEHJCommented:
In that case, see the following:
public interface Constants {
    public enum Info {
        INFO1(4),
        INFO2(5),
        INFO3(6),
        INFO4(7),
        INFO5(8),
        INFO6(9),
        INFO7(10);
 
	public final int index;
 
        Info(int index) {
            this.index = index;
        }
    }
}
 
==================================================================
 
public class TestEnum {
    public static void main(String[] args) {	
	String s = TestEnum.getConstant(Constants.Info.INFO1);
	System.out.println(s);
	System.out.println(Constants.Info.INFO1.index);
 
	//Constants.Info.INFO1.index = 100; << Not allowed (final)
    }
 
    public static String getConstant(Constants.Info index) {
	return index.toString();
    }
}

Open in new window

0
 
arjunarajanAuthor Commented:
Hey,

Its really working great... I appreciate your efforts...

See, again your are directly telling "String s = TestEnum.getConstant(Constants.Info.INFO1);"

here im constructing the INFO1 dynamically. so is it possible to say like this instead of 'String s = TestEnum.getConstant(Constants.Info.INFO1);'

String s = TestEnum.getConstant("Constants.Info." + "INFO" + index);

where index is what i will be getting as input. pls dont refer INFO1, INFO2, etc directly. In otherwords, dont hard code INFO1 directly. Instead INFO1 will be constructed dynamically using the index passed and then I want the value of INFO1.

Hope this will help you to get clear idea.
0
 
CEHJCommented:
Last implementation:
public class TestEnum {
    public static void main(String[] args) {	
	String s = TestEnum.getConstant(Constants.Info.INFO1);
	System.out.println(s);
	System.out.println(Constants.Info.INFO1.index);
	s = TestEnum.getConstantAsString(Integer.parseInt(args[0]));
	System.out.println(s);
	int indexValue = TestEnum.getConstant(Integer.parseInt(args[0])).index;
	System.out.println(indexValue);
 
	//Constants.Info.INFO1.index = 100; << Not allowed (final)
    }
 
    public static String getConstant(Constants.Info index) {
	return index.toString();
    }
    public static String getConstantAsString(int index) {
	return Enum.valueOf(Constants.Info.class, "INFO" + index).toString();
    }
    public static Constants.Info getConstant(int index) {
	return Enum.valueOf(Constants.Info.class, "INFO" + index);
    }
}

Open in new window

0
 
arjunarajanAuthor Commented:
Thank you CEHJ. I appreciate you for providing the solutions at the earliest. Thank you very much.
0
 
CEHJCommented:
:-)
0
 
arjunarajanAuthor Commented:
Hi CEHJ,

I am not familiar with the ENUM in Java. Since, I was bit confused with the approach. Can you please explain me how ENUM and the example you have given will work?

Thank you,
Rameshbabu Arjuna Rajan
0
 
CEHJCommented:
Enum is a type-safe enumeration implemented as an addition to the language in 1.5. Your values can be implemented as the initialization value of the enum member, as i've done here
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now