aspose file tools*
The moose likes Beginning Java and the fly likes returning a char array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "returning a char array" Watch "returning a char array" New topic
Author

returning a char array

Katy Pat
Greenhorn

Joined: Nov 09, 2012
Posts: 4
So, I have a text file containing a series of characters that I need to manipulate later in the program. So right now I'm trying to write a method that puts the chars in the string into a char[], then returns the array. I've tried lots of different stuff, but the following code is the only attempt that didn't have lots of errors, it just doesn't display anything when I call reads(reader) in main.
The mistake is probably obvious, but we just started arrays.

Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

In the String class, there's a method that converts a string to a character array.

With that, all you'd have to read the file as a string then convert it.

Edit:
To print an array you need to put it into a loop.

Also, I would change the way that the file is being read in.
Yours starts at answers[1] rather than answers[0].

I think it would be easier to put it into a while loop when reading a file



Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

Just to be clear, your text file has multiple lines, and you want to return the first letter of each line in a char array? Or you want to return all characters in the file in a char array. It's hard to say without knowing your goal, but I'd guess your main mistake is using hasNext() the way you are.
Katy Pat
Greenhorn

Joined: Nov 09, 2012
Posts: 4
Greg Charles wrote:Just to be clear, your text file has multiple lines, and you want to return the first letter of each line in a char array? Or you want to return all characters in the file in a char array. It's hard to say without knowing your goal, but I'd guess your main mistake is using hasNext() the way you are.


Actually, I'm wanting to grab every other line in the file, each of which contains letters, and return an array containing all of the letters in the line.
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

Just to clarify, you want it to do this?

Read in a file:


and return

in array form?
Katy Pat
Greenhorn

Joined: Nov 09, 2012
Posts: 4
Yes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: returning a char array