• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting String to an Array  RSS feed

 
Dishant Verma
Greenhorn
Posts: 27
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String [] s = str; // ???

I wish to convert this String str into an array of String [] s.
Such that:
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use split method in string class.
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just like below code
 
Dishant Verma
Greenhorn
Posts: 27
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sachin for your response.

What I have done is:


and the output i get is:
 
Harsha Ka
Ranch Hand
Posts: 45
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check your array index. The correct code must be
 
Dishant Verma
Greenhorn
Posts: 27
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Harsha:

When I done this, it then does n't showed me exception. (thank you)

But what I want to do is still not done...

it only prints IJ... not rest of the elements
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you want to print exactly???
 
Dishant Verma
Greenhorn
Posts: 27
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want String like :
to be converted into an array. And then print that array.

 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
means are you want to print just like below output?

Output:

"AB"
"CD"
"EF"
"GH"
"IJ"
 
Dishant Verma
Greenhorn
Posts: 27
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yes,,, but using array...

output:
AB
CD
EF
GH
IJ
 
Dishant Verma
Greenhorn
Posts: 27
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done this in another way using String Tokenizer...


Output:


But i want to do this using array...
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

try this code

 
Dishant Verma
Greenhorn
Posts: 27
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thank you Sachin,

it works perfectly fine as I wanted.

Actually what I was doing is :

and this was throwing exception.

Now as per your code it works fine.

Thank You again.
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thakur Sachin Singh, please DontBeACodeMill(⇐click). It doesn't help somebody if you just write his code for him.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!