?
Solved

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

Posted on 2003-03-19
8
Medium Priority
?
320 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
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month7 days, 6 hours left to enroll

593 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