• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting String to Array Object  RSS feed

 
Prakash Balasubramani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an value in string variable as "arrayname[index]".

How to convert this to an corresponding array object for getting values from the array?

Thanks in advance for ur Help.

B.Prakash.
 
Shawn Rieger
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure i follow your question but.. If your trying to return a specific element in an array...

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to Java in General(beginner)
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know how to do it without specific code like:
String xxx = "anArray[10];


Why would you want to do this?
 
Jim Kiley
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm,

Did you mean parseInt(xxx) where you have parseIndex(xxx) there?

jk
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code was only an example/pseudo code, not something that would compile.
The parseIndex() method would scan the xxx string passed to it and return the value from between the [] as an int. parseIndex() would probably use Integer.parseInt() once the inclosing [] had been found.
 
Prakash Balasubramani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Actually i have many arrays in my file...

I will be getting the array name dynamically so the string can have a values such as a[]/b[] like that so on depends upon the conditions...

So after getting the name of array in string variable i have to fetch the value from the corresponding array..

Hope all would have got my problem rightly..

I want some clearer answers pls..

Thanks in advance for the help

B.Prakash
 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a similar query some time back:
Check out:
http://www.coderanch.com/t/377728/java/java/name-matrix-as-user-input

The answer lies in the use of HashMap class (or any other implementation of the Map interface.)

Hope this works for you!
 
Prakash Balasubramani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I viewed those replies but mine is somewhat different from that. I am not creating any dynamic array.

I have many number of arrays, in which i have values filled in it. I am getting the name of array in a string during runtime. Now i want to convert this string value to array object from which i should able to fetch values.

Is it possible? if yes how?

Hope u would have got my probs rightly now.

Thanks for ur help and reply.

B.Prakash.
 
Daniel Prene
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please post your code?
 
Prakash Balasubramani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Since the related coding is in more than 2 pages... i have given u the coding as like mine which is as follows

String firstnames[] = {"Ram","Ragu","Ravi"};
String lastnames[] = {"Ram","Ragu","Ravi"};
String designation[] = {"CEO","Programmer","Sys Admin"};
String ValueString;

int i = Integer.parseInt(request.getParameter("index"));

String arraynames[] = {"firstnames","lastnames","designation"};

for(int j=0;j<3;j++)
{
out.println(arraynames[i] + "[" + j + "]");
}


While calling the above page if i pass the index as 0 i get the o/p as follows

firstnames[0] firstnames[1] firstnames[2]

for 1 o/p is

lastnames[0] lastnames[1] lastnames[2]

But i want the values which has been filled with this array names.

Similarly 10-15 arrays i have in my own coding and i can get the array name as above said using index. But i could not able to convert the string such as
lastnames[0],
lastnames[1],
lastnames[2]

to array object.

Hope u got the problem rightly.

Thanks in advance for ur help.

B.Prakash
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi One Solution to this is ,
Store these arrays in a collection class like HashMap or Hash table.
so the key in the hash table will be the name you get dynamically like "firstNames","lastNames" etc ... etc & the value will be the corresponding Array object. So get the arrays from the hash using this key

Does this make sense ?
 
Dustinn Yourstone
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another solution to use (I did this to dynamicly create a bunch of vars with xml) is to use things such as the Class and Field objects. I dont think what you are trying to do it actually possible the way you are thinking. You will either have to map it out in your own memory (previous solution) or dynamicly build classes like I said. You cant just make them up in thin air and expect to use them all during runtime.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!