2

I'm trying to implement in java this little project: I want to rename the episodes of an entire season in a series using a text file that has the names of all the episodes in that season. To that end I wrote a code that reads the text file and stores every line of text (the name of an episode) as a string array, where every element of the array stores the name of one episode. Also, I wrote a code that takes the FIRST element of that array (an array called arrayLines[]) and renames a given file. This code works like a charm. What I want to do next is to create a char array for every element in the string array arrLines[]. The pseudo-code i'm thinking to implement is something like this:

for(int i=0; i<arrLines.length; i++){
    char line_i+1[] = arrLines[i];
}

and thus getting many arrays with the names line_1, line_2,..., line_arrLines.length, with the name of every episode stored as a char array.

How can I implement something like this?

2 Answers 2

2

Just use a 2-dimensional char array:

char[][] lines = new char[arrLines.length][];
for (int i = 0; i < arrLines.length; i++) {
    lines[i] = arrLines[i].toCharArray();
}

If you have Java 8, you can use Streams:

char[][] lines = Arrays.stream(arrLines)
        .map(String::toCharArray)
        .toArray(char[][]::new);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot! worked perfectly (both of them)! what's a Stream? first time I've heard about them.
@GabrielEdery it's a broad subject; if you want to read up on it, you can start here, here and here.
0

You can use the String toCharArra method.

    for(int i = 0; i < strArray.length; i++) {
        char[] charArray = strArray[i].toCharArray();
    }

Comments

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.