• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert String array to char array  RSS feed

 
Rahul Dayal Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an array of Strings, one on each line and I need to convert them into an array of char's.

For Example:

This
is
an
Example
of
what
my
input
is.



In order to accomplish that I did the following-



But whenever I try this and then try to print the output :



I get nothing. I'm not sure what's the flaw in my logic, is it the char array initialization that's wrong or is it something else ?

Can someone please provide the code to rectify this error ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code has an error in it:


Can you explain why you used j (vs i) for both of those calls? (One is wrong and by explaining it, I think you'll see why)
 
Rahul Dayal Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, my Mistake, That was a typo on the forum on my behalf... it's



Extremely sorry for that blunder.

Its the same code with this edit. I'm not able to convert it from a string array to a char array.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. There is a less obvious problem here:


What happens if you have two Strings in lotsOfText that are 100 characters long? The array isn't big enough to hold them.

Are you allowed to use an ArrayList? If not, I think you'll need another set of loops to figure out how big to make the arrays before the code you already have.
 
Rahul Dayal Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I'm fairly new to Java and am not confident with Arraylist. As far as the char initialization is concerned, I had my doubts on that part but am not sure as to how I may bypass that part. Any help with the code please ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about the rest for a moment. Suppose, you had the task to print out the length of the longest String in lotsOfText? Can you do that? (Try and post here)

Hint: it requires a loop and a helper variable.

Once you can do this, you can pass that value to your existing array declaration.
 
Rahul Dayal Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this it ?


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works. You could make it simpler though. String has a method to get the length so no need for the while loop.
 
Rahul Dayal Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would the loop look then ? Can you please show that in code ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You actually almost had it. Just replace:


with:


You knew the method to call. A String can't have null characters inside of it, so there is no need to loop through each character to count.
 
Rahul Dayal Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I did figure that out and decided to try it out on Eclipse. But I got caught in another paradigm of this code....



The var = lotsoftext.charat part is always giving this=

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is still wrong:


count needs to be a single integer, not an array. As in the above code that you wrote and I updated/refactored.

If it is an array, you have the same problem you started with. That it is the # of Strings and not the maximum length of the Strings
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!