I tried to make a program that separates characters.
The question is:
“Create a char array and use an array initializer to initialize the array with the characters in the string ‘Hi there’. Display the contents of the array using a for-statement. Separate each character in the array with a space”.
The program I made:
String ini = "Hi there";
char[] array = new char[ini.length()];
for(int count=0;count<array.length;count++) {
System.out.print(" "+array[count]);
}
What should I do to fix this problem?
Here’s how you convert a String to a char array:
I’d recommend that you use an IDE when programming, to easily see which methods a class contains (in this case you’d be able to find
toCharArray()) and compile errors like the one you have above. You should also familiarize yourself with the documentation, which in this case would be this String documentation.Also, always post which compile errors you’re getting. In this case it was easy to spot, but when it isn’t you won’t be able to get any answers if you don’t include it in the post.