0
public static void main(String[] args) throws IOException {

    String token1 = "";
    Scanner inFile = new Scanner(new File(filename));
    List<String> temps = new LinkedList<String>();

    while (inFile.hasNext()) {
      token1 = inFile.next();
      temps.add(token1);
    }

    inFile.close();

    String[] tempsArray = temps.toArray(new String[0]);

    for (String hole : tempsArray) {
        System.out.println(hole);
    }

I read my file to array, but to continue I need to convert this array to char array. Thanks in advance.

5
  • 1
    For each String in tempsArray: tempArrayString.toCharArray();. Now, why would you need to convert it to char[]? Commented Jun 4, 2014 at 20:50
  • I am trying to finish this : stackoverflow.com/questions/24036201/… Commented Jun 4, 2014 at 20:51
  • 2
    Let's say your string array is ["hello", "world"]. What would you want to transform it to? Commented Jun 4, 2014 at 20:52
  • Maybe you have your own ideas about it ? Commented Jun 4, 2014 at 20:56
  • So your real problem is how to read the data in a text file to create a maze structure type (or similar) in your application to evaluate it. Commented Jun 4, 2014 at 20:59

2 Answers 2

2

It would be easier to start with List<char[]> variable to shorten the code:

Scanner inFile = new Scanner(new File(filename));
List<char[]> temps = new ArrayList<char[]>();
while (inFile.hasNext()) {
    temps.add(inFile.nextLine().toCharArray());
}
inFile.close();
char[][] wholeData = temps.toArray(new char[0][]);
//just to show the data
System.out.println(Arrays.deepToString(wholeData));
Sign up to request clarification or add additional context in comments.

Comments

0

The toCharArray() method of string will return an array of char[] the length of the string.

1 Comment

This should be a comment rather than an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.