Reference to variables using method w/ string parameter?

I've done some programming before, but not too much in Java, so bear with me please.
Basically, what I'm trying to do is find a static method with a string parameter that can serve as a variable reference. Here's an example of what I mean.
Say that the method is "toVariableReference", with a string paramter. Then, to refer to a variable x1, I could refer to it as follows:
x1
OR
toVariableReference("x1")
See?
Now, if you don't see the point yet, the reason I'm trying to figure this out is because if I wanted to check if nine integer primitive data identifiers contain values greater than 500, instead of having 9 if statements, I could do the following:
for (x=1; x<10; x++)
if (toVariableReference("int"+x)>500)
<statements>
Since this theoretical method has a string parameter, one could concatenate an integer counter to it, as I have done above.
Now, the big question. Is there such a method that, when invoked, does what I have described? If so, what is it, and what java packages would I need to import for its usage?
Thank you for your time!
-Katsarephat

Yes, you can change array entries. You might want to take a look at the Java Tutorial:
http://java.sun.com/docs/books/tutorial/
The "Learning the Java Language" link has some information about arrays (and many other things).

Similar Messages

  • Using a string to reference a variable

    hi all,
    i've encountered an interesting scenario where i need to use a string to reeference the value of a variable...the following would show the scenario in a simpler way...
    let's say we have such function called foo:
    void foo(String aString)
    int a = 20;
    int b = 20;
    system.out.println(?????);
    now let's say i run the function as:
    foo("a")
    and i want it to return:
    20
    what should be put to replace those "?????" in my function above?
    please note that variables "a" and "b" are arbitary and so having a conditional statement like the following would NOT be useful:
    void foo(String aString)
    int a = 20;
    int b = 20;
    if (aString.equals("a"))
    system.out.println(a);
    if (aString.equals("b"))
    system.out.println(b);
    thank you very much!

    There are two possibilities here.
    The preferable one, if you have control over the names and nature of the variables, then don't use individual variables - use a Hashtable (or any implementation of Map). Your code would then look something like:void foo(String aString)
        Map table = new Hashtable();
        table.put("a", new Integer(20));
        table.put("b", new Integer(30));
        system.out.println(table.get(aString));
    }The down side to this is that you have to use Integers - Maps can't take primitives.
    If, on the other hand, you have to have variables, then take a look at using reflection. It's more complicated than using Maps, and less efficient, so I would recommend the first technique if possible.
    RObin

  • Setting VarChar Variable with String Parameter in Crystal Report Add Command

    Setting a VarChar Variable with a String Parameter in a Crystal Report 2008 SP6.3 Add Command is causing an error with a SQL State of 42000.  The Error Code depends on which value I select for the parameter.  I created the @DateOpt string parameter in the Modify Command window, not in the report. If I comment out line 14 of the SQL the error does not occur.
    I have attached the SQL Code and screen shots of the errors.   Please tell me what I am doing wrong.

    Since your parameter is of type STRING, you need to enclose references to it within the Command inside single quotes.
    For example, instead of
    SET @BeginDate ={?BeginDate}
    use
    SET @BeginDate ='{?BeginDate}'
    hth,
    - ido

  • Member variable verses method parameter to pass information

    Hi all,
    Is the a performance penalty in using method parameter, such as a String, to pass information into multiple methods( method1(String X), method2(String X), etc ) in a class verses using a class member variable to pass the information to the methods?
    Thanks.

    Never, ever, ever make a decision as to what should be parameter and what should be field based on this kind issue. If the value is reasoably part of the state of the object it should stored (or referenced by) a field. Otherwise it should not and so your left with it being a parameter or an atribute of some other object.
    There is little if any performance cost in passing a parameter (on the order of 10's to 100's of nanoseconds on modern computers and JVM's. Optimizing in this area will only noticeably impact performance of such calls if it needs to get performed 100's of thousands or millions of times per second. That generally excludes everything any of us is likely to write.
    Chuck

  • How to know  pro-grammatically variable type(string ,numeric ....) of TestStand variable using CVI

    Hi,
    how to know  pro-grammatically  variable type(string ,numeric ....)  of TestStand  variable using Labwindows CVI.
    Thanks
    Mukesh Kumar

    Mukesh,
    You could use the TS_PropertyGetType() function in CVI to do this. Here is a reference page for the GetType function--you can see that it returns a PropertyValueType which will indicate which type the PropertyObject (variable) is. In CVI, you'll find this function in the NI TestStand API 2013 instrument methods, under PropertyObject > Static Properties. 
    Hope it helps, and let us know if you have any more questions about it!
    Daniel E.
    TestStand Product Support Engineer
    National Instruments

  • Using this as String reference

    Hi friends,
    Can anybody help find my mistake in the following code.?
    public class ThisCheck
    { public ThisCheck()
    {System.out.printf("%s",this);
    public String str()
    {return "shiva";
    public static void main(String a[])
    { ThisCheck the=new ThisCheck();
    the output I am expecting is Shiva.
    But the output Iam getting is the address of the object ThisCheck
    as ThisCheck@7d772e
    What's going wrong?
    I appreciate ur help.

A: using this as String reference

Try:public class ThisCheck {
    public ThisCheck() {
        System.out.printf("%s", this);
    public String toString() {
        return "shiva";
    public static void main(String a[]) {
        ThisCheck the = new ThisCheck();
}The %s flag in printf() looks for the toString() method of the corresponding
argument. (Details here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html)
When you post it's a good idea to use the Formatting tips
http://forum.java.sun.com/help.jspa?sec=formatting
Basically it means putting [code] at the start of your code and [/code] at
the end.

Try:public class ThisCheck {
    public ThisCheck() {
        System.out.printf("%s", this);
    public String toString() {
        return "shiva";
    public static void main(String a[]) {
        ThisCheck the = new ThisCheck();
}The %s flag in printf() looks for the toString() method of the corresponding
argument. (Details here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html)
When you post it's a good idea to use the Formatting tips
http://forum.java.sun.com/help.jspa?sec=formatting
Basically it means putting [code] at the start of your code and [/code] at
the end.

  • Can a single quote be used at the beginning of a query string parameter

    Hi all,
    I am a relative newbie and have a newbie question.
    Can a single quote be used at the beginning of a query string parameter passed to a jsp page? Are there any inherant problems with this? Is there a comprehensive list of characters that must be escaped in a query string parameter?
    Example: http://mysite.com/myjsp.jsp?param1='nghdh
    Thanks

    You'll have to escape most non-letter characters before you can pass them as a URL. I don't know if it's necessary for a single quote, but better safe than sorry.
    Either use java.net.URLEncoder(...) or use javax.servlet.http.HttpServletResponse.encodeURL(String). I wouldn't recommend using unescaped characters in your URLs, that might cause pretty funny behavior that's sometimes hard to trace back. Don't worry about decoding it, your JSP/Servlet container will do it when you call javax.servlet.http.HttpServletRequest.getParameter(String).

  • I'm using TestStand/Labview to do a string value test. Is there any way to use a variable in the edit string value test?? This forces you to hard code a string to test against.

    I'm using TestStand 2.0/Labview 6i to do a string value test. Is there any way to use a string variable in the edit string value test instead of an actual string?? This forces you to hard code a string to test against.

    Hi ART,
    You can also use the LimitLoader step to load your string into to step similar to the Numeric Step type.
    There should be an example of this in the Resource Library | TestStand
    Regards
    Ray Farmer
    Regards
    Ray Farmer

  • Using Java Reflection to call a method with int parameter

    Hi,
    Could someone please tell me how can i use the invoke() of the Method class to call an method wiht int parameter? The invoke() takes an array of Object, but I need to pass in an array of int.
    For example I have a setI(int i) in my class.
    Class[] INT_PARAMETER_TYPES = new Class[] {Integer.TYPE };
    Method method = targetClass.getMethod(methodName, INT_PARAMETER_TYPES);
    Object[] args = new Object[] {4}; // won't work, type mismatch
    int[] args = new int[] {4}; // won't work, type mismatch
    method.invoke(target, args);
    thanks for any help.

    Object[] args = new Object[] {4}; // won't work, type
    mismatchShould be:
        Object[] args = new Object[] { new Integer(4) };The relevant part of the JavaDoc for Method.invoke(): "If the corresponding formal parameter has a primitive type, an unwrapping conversion is attempted to convert the object value to a value of a primitive type. If this attempt fails, the invocation throws an IllegalArgumentException. "
    I suppose you could pass in any Number (eg, Double instead of Integer), but that's just speculation.

  • What is the use of passing String[] args in main() method?

    what is the use of passing String[] args in main() method?
    Is there any specific use for this ?

    actually my sir asked me the same question & I gave
    the same reply as given by you........but he further
    asked what is the use of this also??
    ie accepting cmd line args at runtime??is there any
    specific purpose ??Apart from the one you just mentioned? No

  • How to create dynamic connection string with variables using ssis.

    Hello,
    Can anyone let me know on how to create dynamic connection string with variables using ssis?
    Any help would be appreciated.

    Hi vinay9738,
    According to your description, you want to connect multiple database from multiple servers using dynamic connection.
    If in this case, we can create a Table in our local database (whatever DB we want) and load all the connection strings.  We can use Execute SQL Task to query all the connection strings and store the result-set in a variable of object type in SSIS package.
    Then use ForEach Loop container to shred the content of the object variable and iterate through each of the connection strings. And then Place an Execute SQL task inside ForEach Loop container with the SQL statements we have to run in all the DB instances. 
    For more details, please refer to the following blog:
    http://sql-developers.blogspot.kr/2010/07/dynamic-database-connection-using-ssis.html
    If there are any other questions, please feel free to let me know.
    Regards,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • RFC FM using string parameter

    Hi all
       An RFC FM can using string parameter? why i using string pass value ,the destination reciev the value is wrong?
    thanks
    BR
    Chris

    Are you using any std func.. just wanted to chek that there is no internal conversion routine....
    Enjoy SAP.
    Pankaj Singh

  • FileName using Variable Substitution Method

    Hi SAP experts,
    I need some advise regarding this.
    I have here sample structure of message,
    DT_Root  --> root node
      Tree  --> occurence is 1 to unbounded
        SubTree1
        SubTree2
        SubTree3
    What will be my File Content Conversion parameters to be set in Variable Substitution Method in order for me to get the value of SubTree3 and make it as my filename?
    Also, the value of SubTree3 must not be present on my actual payload..
    var1:payload,1,SLI,1   ---?? what if it is unbounded?
    Kindly advise.
    Thank you very much.

    Hi Raj,
    I cannot open the link.
    Anyway, is it ok if you will advise me regarding my question a while ago?
    I have here sample message structure,
    CSV --> Message Type
    SLI  --> 1 .. unbounded (occurence)
      subSLI1
      subSLI2
      subSLI3
    FileNode --> 1..1 (occurence)
      FileName --> 1..1
    Here's my parameters in Variable Substituion method,
    FileName Scheme: %var1%.csv
    var1   --  payload:CSV,1,FileNode,1,FileName,1
    File Content Conversion Parameters:
    Recordset Structure: SLI,FileNode
    SLI.addHeaderLine: 3
    SLI.headerLine: PERIO;EAN;ILN,....
    SLI.fieldFixedLengths: 8;10;13;...
    SLI.fixedLengthTooShortHandling: Cut
    SLI.fieldSeparator: ;
    SLI.endSeparator: 'nl'
    FileNode.fieldFixedLengths: 0
    FileNode.fixedLengthTooShortHandling: Cut
    I must the output flatfile with a value of the filename in the FileName field.
    And the content of the flatfile must look like this,'
    PERIO;EAN;ILN;SALES;TURNO;PERFL;SUBSFL;SALUN;%CURRENCY%;STOCK;STOUN;ARTNR;ARKTX
    20071020;737052020198 ;4333342000008;         1;        9.95;W;X;ST ;EUR;0000000000;ST ;                                  
    20071020;737052056852 ;4333342000008;         1;       14.50;W;X;ST ;EUR;0000000000;ST ;                                  
    20071020;737052073552 ;4333342000008;         1;       12.95;W;X;ST ;EUR;0000000000;ST ;                                  
    *each field has a fixed field length.
    Kindly advise if my set parameters are correct.
    Thank you very much!

  • Find the path of a string using methods

    Dear Gurus ,
    I have the path "
    filestltrv\sap\mauals\text.xls' and i want using Methods in ABAP  to find the path '"
    filestltrv\sap\mauals\'.
    I suppose using loops and with finding '\' i will do it .
    Is any other Way ????
    Thanks a lot !!!!

    Hello,
    The FM: SO_SPLIT_FILE_AND_PATH is what you are looking for.
    BR,
    Suhas

  • How to use method String.split

    I am spliting a string, just like "1a|24|3|4". My code is
    String[] array = new String[10];
    String buff = "1|2|3|4";
    array = buff.split("|");
    System.out.println(array[1]);
    I think that the result should be "24", but the result always
    is "1". Why? When I use String.split(",") to split a string "1a,24,3,4",
    I can get the result "24". Can't "|" be used as a delimiter? Who can
    explain this issue? Thanks.

    Hi
    The argument of split is a "regular expression" instead any common string with delimiters as used in StringTokenizer.
    The char "|" is named as "branching operator" or "alternation"..doesn't matter this moment.
    If you must to use "|" in source strings, change the regular expression to "\\D", it means "any non numeric char":
    // can use any delimiter not in range '0'..'9'
    array = buff.split("\\D");
    Regards.

  • Maybe you are looking for

    • Consuming ABAP Webservice in Webdynpro app

      Hi, I have wrote a webdynpro app for consuming ABAP webservice. I used the procedure in the link below http://wiki.sdn.sap.com/wiki/display/WDJava/ConsumingABAPWebServiceinJavaWD Everything seemed to be went well, I created model and Webservice desti

    • Foxconn 593-0538 A

      Question: Is this cable related only to the Webcam or also to the LCD. If I don't connect this cable will the monitor work? Thanks Davide

    • Indesign, repeat image to create pattern

      Is there a way to tile an image in Indesign, like you can in Photoshop/patterns to Illustrator/patterns? I don't want to create an image, I want to import an image and how it repeat. Why? To keep file sizes down and to speed up work.

    • Windows Server 2008 Standard 64Bit

      We need Windows 2008 Standard 64 bit. So please convert Windows 2008 Standard 32bit to Windows 2008 Standard 64bit. Please send me the link of Windows 2008 Standard 64Bit ISO Image File  Regards  Sandeep Sahadevan

    • How to manage focus in custom component

      Hallo. As you know some flex components have native focus, like buttons, textinput etc.. I have created a custom component which is a borderContainer.. As you know, by default, if user press TAB my BorderContainer will never receive the focus.. I try