Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Using a variable in an identifier
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
guttface




PostPosted: Fri Oct 12, 2007 1:34 pm   Post subject: 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?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Oct 12, 2007 1:51 pm   Post subject: RE:Using a variable in an identifier

Why not just use an array?
guttface




PostPosted: Fri Oct 12, 2007 1:54 pm   Post subject: 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




PostPosted: Fri Oct 12, 2007 1:56 pm   Post subject: RE:Using a variable in an identifier

is someone expecting to see an object with a method for each String? Neutral
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
guttface




PostPosted: Fri Oct 12, 2007 2:03 pm   Post subject: 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




PostPosted: Fri Oct 12, 2007 2:31 pm   Post subject: 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"

Java:

 import java.lang.reflect.*;
       
   public class method2 {
      public int add(int a, int b)
      {
         return a + b;
      }
       
      public static void main(String args[])
      {
         try {
           Class cls = Class.forName("method2");
           Class partypes[] = new Class[2];
            partypes[0] = Integer.TYPE;
            partypes[1] = Integer.TYPE;
            Method meth = cls.getMethod(
              "add", partypes);
            method2 methobj = new method2();
            Object arglist[] = new Object[2];
            arglist[0] = new Integer(37);
            arglist[1] = new Integer(47);
            Object retobj
              = meth.invoke(methobj, arglist);
            Integer retval = (Integer)retobj;
            System.out.println(retval.intValue());
         }
         catch (Throwable e) {
            System.err.println(e);
         }
      }
   }
guttface




PostPosted: Fri Oct 12, 2007 7:46 pm   Post subject: RE:Using a variable in an identifier

Is there some way to wrap the name of the string in an object?
Euphoracle




PostPosted: Fri Oct 12, 2007 8:14 pm   Post subject: 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].
Sponsor
Sponsor
Sponsor
sponsor
guttface




PostPosted: Sat Oct 13, 2007 1:08 am   Post subject: Re: Using a variable in an identifier

is there no way of doing the following simply:

code:
for(int i=1;i<6;1++){
     ....
     SomeStrings.["s"+i].length();
     ...
}
wtd




PostPosted: Sat Oct 13, 2007 1:16 am   Post subject: RE:Using a variable in an identifier

No.

What possible kind of exercise asks you to do something like this, and restricts you from using arrays or some other form of aggregate data structure?
Euphoracle




PostPosted: Sat Oct 13, 2007 2:07 am   Post subject: RE:Using a variable in an identifier

The ones my teacher gives us too, copypasta assignments. Mmmm. And docking marks for not including the libraries they tell us to even when we don't need them, now that's the sauce.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: