I'm new to Java and stuck on the code below. I don't know how to return the char array; and also if I change the string "purple"
to something else, Java won't compile the code.
public class Assigment4 {
public static void main(String[] args) {
// I get an error if color is initialized with a longer or shorter string.
String color = "purple";
char[] a = turnStringtoChar(color);
System.out.println(a);
}
public static char[] turnStringtoChar(String color) {
char[] letters = {'p', 'u', 'r', 'p', 'l', 'e'};
for (int i = 0; i < color.length(); i++) {
// This is the part where I am stuck. I don't know what to return.
letters[i] = color.charAt(i);
}
return letters;
}
}
Can anyone help me?