Forums Register Login

returning an array of characters

+Pie Number of slices to send: Send
I'm trying to write a method that returns the individual characters of a certain string. For example, is String s = "Adam" then c[0] = "A", c[1] = "d" and so on.

I'm not sure how to use a return statement to do this. Here is the method:


[ December 02, 2004: Message edited by: Adam Blais ]
+Pie Number of slices to send: Send
You need a return statement at the end of your method to return the char array. Also you can eliminate the endloop int variable and just declare your for loop like this.
+Pie Number of slices to send: Send
Thanks for the response. My problem is I don't know how to use a return statement to return an array. I thought is was return output[];, but that isn't working. What's the correct command to get this array returned?
[ December 02, 2004: Message edited by: Adam Blais ]
+Pie Number of slices to send: Send
Your identifier for the char[] is just "output," so all you need is:

return output;

(Note that there's a method in String that already does this. It's toCharArray().)
+Pie Number of slices to send: Send
Thanks Marc, it works fine now.

I'm doing my next exercise, and I need to convert a character array to a string. When I try to use the method in BlueJ and enter the character array with {a, b, c}, it says that it cannot resolve variable a. If I put {"a", "b", "c"}, it says it found a string where a char was expected.
How do I enter in an array?
+Pie Number of slices to send: Send
{'a', 'b', 'c'}

Use single quotes, not double....
+Pie Number of slices to send: Send
Haha thanks.

Can someone tell me why I'm getting a "possible loss of precision" error in the Math.pow line?

+Pie Number of slices to send: Send
Math.pow() returns a double, which you're trying to assign to an int. You'll need to explicitly cast the result to type int...

powerOfTwo[k] = (int)Math.pow(2, k);
[ December 02, 2004: Message edited by: marc weber ]
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 8392 times.
Similar Threads
Bytearray to ascii string? Remove padding?
Concatenate all of the strings in an array
String help!
problem with ArrayList method
Removing Characters from String
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:25:11.