This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked 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.
Also, this is the first time I've posted in this forum, so am unsure how to format it properly. I know it sucks trying to read unindented code, but I honestly have no idea.



import java.io.*;
import java.util.Scanner;


public class Practice3
{
public static void main(String[] args) throws IOException {
Scanner reader = new Scanner(new File("answers.txt"));
System.out.println(reads(reader));
}

public static char[] reads(Scanner reader) {
int x = 0;
String line = reader.nextLine();
char[] answers = new char[line.length()];
while(reader.hasNext() && x <= line.length()-1) {
x++;
answers[x] = line.charAt(x);
}
return answers;
}
}
 
wood burning stoves
 
subject: returning a char array
 
Similar Threads
Java Encrypt and decrypt file
Scanning/Storing a String that the user enters?
Letter counter program. having trouble understanding it.
Hard time filling array
Count the number of vowels, words, and sentences.