• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert a String into a two dimension array ?  RSS feed

 
sharad jash
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to convert a String (say str) contains sum values like str = "abcdefghighijklmnopqrstuvwxyz" and I want it to convert it into an array say sdata[100][4].
(Taking 100 and 4 as example)
the values of sdata will be like
sdata[0] = {'a','b','c','d'};
sdata[1] = {'e','f','g','h'};
[Fixed code tags - see UseCodeTags for instructions]

Anyone please help ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exact help do you need? Generally, you need to ask a specific question. I'd suggest you read some of our FAQ on HowToAskQuestionsOnJavaRanch.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use toCharArray() on substrings of the initial string to load up your 2d character array. You'll want a for loop to load it up while the string size is divisible by whatever char array size you want (4 in your text example), then the remainder should be done with just a couple lines of code.

Edit: this problem was irking me a bit so I decided to give it a formal attempt. It would be a lot easier if the string size were divisible by the array size.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!