Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

returning an array of characters

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().)
 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
{'a', 'b', 'c'}

Use single quotes, not double....
 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haha thanks.

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

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic