Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning a char array

 
Katy Pat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify, you want it to do this?

Read in a file:


and return

in array form?
 
Katy Pat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic