This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

returning an array of characters  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
{'a', 'b', 'c'}

Use single quotes, not double....
 
Adam Blais
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!