
-----------------------------------
guttface
Fri Oct 12, 2007 1:34 pm

Using a variable in an identifier
-----------------------------------
I'm trying to read several strings from a class called SomeStrings. There are 5 strings which need to be read, s1,s2,s3,s4,s5. I want to use a for loop to initiate a scanner object for each string eg 

Scanner scan=new Scanner(SomeStrings.s1);

except instead of s1 i want a variable name so that i can create each new scanner inside a for loop. How can I do this?

-----------------------------------
wtd
Fri Oct 12, 2007 1:51 pm

RE:Using a variable in an identifier
-----------------------------------
Why not just use an array?

-----------------------------------
guttface
Fri Oct 12, 2007 1:54 pm

RE:Using a variable in an identifier
-----------------------------------
not allowed. i need something like SomeStrings.nameOfString where nameOfString evaluates to s1,s2,s3,s4,s5.

-----------------------------------
Tony
Fri Oct 12, 2007 1:56 pm

RE:Using a variable in an identifier
-----------------------------------
is someone expecting to see an object with a method for each String? :|

-----------------------------------
guttface
Fri Oct 12, 2007 2:03 pm

RE:Using a variable in an identifier
-----------------------------------
I'm starting out in Java and not 100% sure what that means. To clarify I need to perform certain operations on 5 different strings. I can't copy and paste the code to perform those operations 5 times, but rather I have to have 1 copy of the code inside a loop. Everytime through the loop i need to refer to the next string. The first time I need SomeStrings.s1, the next time SOmeStrings.s2. How can I replace the s1, s2 with a variable?

-----------------------------------
Euphoracle
Fri Oct 12, 2007 2:31 pm

RE:Using a variable in an identifier
-----------------------------------
Well, unless you're allowed to use any advanced concepts, you're going to have to copy & paste.  However, for reference to anyone else who needs to solve this and can use advanced concepts, you can use reflection to call the method based on a string name, using a for-loop to get the numbers to append to the end of "s".

Example, this will invoke the "add" method of class "method2"


 import java.lang.reflect.*;
        
   public class method2 {
      public int add(int a, int b)
      {
         return a + b;
      }
        
      public static void main(String args

-----------------------------------
guttface
Fri Oct 12, 2007 7:46 pm

RE:Using a variable in an identifier
-----------------------------------
Is there some way to wrap the name of the string in an object?

-----------------------------------
Euphoracle
Fri Oct 12, 2007 8:14 pm

RE:Using a variable in an identifier
-----------------------------------
I'm not sure what you mean.  You could create a class and make a new public string for each one, if you want to do it the "SomeStrings.s1" method, or you could use an array:

String[] s = { "first", "second", "third" };

Arrays are 0-based though, so if you want "first" you need to use s[0].

-----------------------------------
guttface
Sat Oct 13, 2007 1:08 am

Re: Using a variable in an identifier
-----------------------------------
is there no way of doing the following simply:

for(int i=1;i