7

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?

Community
  • 1
  • 1
Ahmadz Issa
  • 669
  • 3
  • 12
  • 36
  • Possible duplicate of [How to init char array using char literals?](http://stackoverflow.com/questions/11711228/how-to-init-char-array-using-char-literals) – Stephan Mar 02 '16 at 09:30
  • https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#fill(char[],%20int,%20int,%20char) - if you want all to be the same. – JGFMK May 21 '20 at 19:19

7 Answers7

18

Here's how you convert a String to a char array:

String str = "someString"; 
char[] charArray = str.toCharArray();

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.

keyser
  • 18,829
  • 16
  • 59
  • 101
1

you are doing it wrong, you have first split the string using space as a delimiter using String.split() and populate the char array with charcters.

or even simpler just use String.charAt() in the loop to populate array like below:

String ini="Hi there";
  char[] array=new  char[ini.length()];

  for(int count=0;count<array.length;count++){
         array[count] = ini.charAt(count);
  System.out.print(" "+array[count]);
  }

or one liner would be

  String ini="Hi there";
  char[] array=ini.toCharArray();
PermGenError
  • 45,977
  • 8
  • 87
  • 106
1
char array[] = new String("Hi there").toCharArray();
for(char c : array)
    System.out.print(c + " ");
Nate Yost
  • 11
  • 1
  • 2
    A straight code answer offers the OP little information about your resolution or why is works. Please give some sort of explanation or narrative. – m_callens Mar 15 '17 at 18:15
0

Here is the code

String str = "Hi There";
char[] arr = str.toCharArray();

for(int i=0;i<arr.length;i++)
    System.out.print(" "+arr[i]);
Pratik
  • 30,639
  • 18
  • 84
  • 159
0

Instead of above way u can achieve the solution simply by following method..

public static void main(String args[]) {
    String ini = "Hi there";
    for (int i = 0; i < ini.length(); i++) {
        System.out.print(" " + ini.charAt(i));
    }
}
Raja P
  • 3
  • 6
0

You initialized and declared your String to "Hi there", initialized your char[] array with the correct size, and you began a loop over the length of the array which prints an empty string combined with a given element being looked at in the array. At which point did you factor in the functionality to put in the characters from the String into the array?

When you attempt to print each element in the array, you print an empty String, since you're adding 'nothing' to an empty String, and since there was no functionality to add in the characters from the input String to the array. You have everything around it correctly implemented, though. This is the code that should go after you initialize the array, but before the for-loop that iterates over the array to print out the elements.

for (int count = 0; count < ini.length(); count++) {
  array[count] = ini.charAt(count);
}

It would be more efficient to just combine the for-loops to print each character out right after you put it into the array.

for (int count = 0; count < ini.length(); count++) {
  array[count] = ini.charAt(count);
  System.out.println(array[count]);
}

At this point, you're probably wondering why even put it in a char[] when I can just print them using the reference to the String object ini itself.

String ini = "Hi there";

for (int count = 0; count < ini.length(); count++) {
   System.out.println(ini.charAt(count));
}

Definitely read about Java Strings. They're fascinating and work pretty well, in my opinion. Here's a decent link: https://www.javatpoint.com/java-string

String ini = "Hi there"; // stored in String constant pool

is stored differently in memory than

String ini = new String("Hi there"); // stored in heap memory and String constant pool

, which is stored differently than

char[] inichar = new char[]{"H", "i", " ", "t", "h", "e", "r", "e"};
String ini = new String(inichar); // converts from char array to string

.

Vik
  • 1
0

Another easy way is that you use string literal and convert it to charArray in declaration itself.

Something like this -

char array[] = "Hi there".toCharArray();

//Print with white spaces
for(char c : array)
   System.out.print(c + " ");