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
arjunarajanAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
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
 
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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

Open in new window

0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.