?
Solved

Is there a way to create a dynamic variable name in Java?

Posted on 2003-03-19
8
Medium Priority
?
315 Views
Last Modified: 2010-03-31
I have a webpage that is generated through a script. Let's say it has a column of text boxes with which is generated depending on the results returned from a search.  There are up to 100 text boxes that can be generated on the page.  I want to get each box name on the form and add it to a vector.

Naming convention is the following:  window.document.F1.t_ +the row number.  

Instead of doing
vector.addElement(window.document.F1.t_1);
vector.addElement(window.document.F1.t_2);
vector.addElement(window.document.F1.t_3);
...

I would like to do something like this:

for (int counter=1;counter<=10;counter++)
{  vector.addElement(window.document.F1.t_counter)
}

Is it possible to do something like this in java?
0
Comment
Question by:Dmitriy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:allahabad
ID: 8168679
Vector compContainer = new Vector();
for (int counter=1;counter<=100;counter++)   {
   compContainer.addElement("window.document.F1.t_"+String.valueOf(counter));
 }
0
 
LVL 3

Author Comment

by:Dmitriy
ID: 8169709
allahabad,

I am sorry I didn't make the question clearer.  I didn't mention that I would want to do something like this later:

for (i=0;i<vectorSize;i++)
{   if(vector.getElementAt(i).value = test)
       System.out.println("Test")

    else
       System.exit(0)
}

I will need to have an access to the values of the text boxes later.
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8170006
You can acess those , once you have stored them in any container(in this case Vector).
Vector compContainer = new Vector();
              for (int counter=1;counter<=100;counter++)   {
                compContainer.addElement("window.document.F1.t_"+String.valueOf(counter));
              }

//Here textfield names are stored in Vector.
// Acess those from the vector
// do want this ?
for (int i=0 ; i<compContainer.size();i++) {
  System.out.println((String) compContainer.elementAt(i));
}
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 3

Author Comment

by:Dmitriy
ID: 8170461
Actually, I need to know what is inside those text boxes.  I am working with javascript and need to get the values in the textboxes,not their names, and verify that the values they have are all length of 8.
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8170508
You asked,  I want to get each box name on the form and add it to a vector. , and your example also had name of text box. Is that not correct ?
Ok, all right.

Do you know values for  each text field, if you know  then you can store them  in the Hashtable as key value pair.  Key would be name of the field.

You can retreive those values from the methods available in Hashtable and check the length. Let me know , if you have problem.
0
 
LVL 1

Expert Comment

by:gwang77
ID: 8171195
if javascript, can use the follow:
for(var i=0;i<document.forms[0].elements.length;i++)
{
 if(document.forms[0].elements[i].type=="text")//can also add some condition such as name=="somename"
 {
  alert(document.forms[0].elements[i].value);
 }
}

if you want to check the value at server, you need submit the values to server, then at server get the values, use java check it.
0
 
LVL 4

Accepted Solution

by:
kokchoon78 earned 300 total points
ID: 8171328
Hi,

  The only way to get the value from the html text box is to use request.getparameter() method. i assume that you have a html form with 100 textbox, and you submit it to a jsp or servlet, then you can use :

Vector vector = new Vector();
for ( int i = 0 ; i < 100; i ++ )
{
   String data = request.getParameter("t_" + Integer.toString( i + 1 ) );
   if( data == null ) data = "";
   vector.addElement( data );
}

hope this is what you are requested.

regards,
Kok Choon
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8171975
This question belongs in the Javascript area, not Java:

but, this is easy.
When you dynamically generate the input elements, override "onBlur" to call a simple javascript method that verifies the element length. like   onBlur="checkLength(this);".  if the lenght check fails, return focus to 'this' element.

or loose the type=submit button for a type=button onClick="validateForm();" which calls document.formName.submit(); after validating lengths.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question