hello, guys i have a really stupid problem about characters and string; i dont know which to use. wacko.gif
iam bubble sorting for example a name::'( :?:

input:

dreamincode


output:

a c d d e e i m n o

why does my code in error? my compiler saids the ff:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The local variable characterArray may not have been initialized
    The local variable characterArray may not have been initialized
    The local variable characterArray may not have been initialized

    at BubbleSort.main(BubbleSort.java:15)


i try my hardest but as if i try the more error i get.
i try to simplify the error like this short so it wouldn't be so complicated.

import java.util.Arrays;
import java.io.*;

public class BubbleSort {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException{
		BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
		char characterArray[];
		
		System.out.print("Enter Name:\t");
		
		Arrays.sort(characterArray);

        for (int i = 0; i < characterArray.length; i++)
        {
             System.out.print(characterArray[i]);
             System.out.print(", ");
        }
		

	}

}

can you please help me??
i really need your help.
Thnks so much

Recommended Answers

All 5 Replies

You need to initialize your character array something like this:-

char characterArray[] = new char[charArraySize];

where the "charArraySize" implies the dimension for your array, you can replace it with 10,20, 30 or ask the user to input it.

Also how come I do not see the line where you are taking the input from the console ??

You need to initialize your character array something like this:-
Also how come I do not see the line where you are taking the input from the console ??

i must have erased it so thats why my code is very complicated i forgot
and oh! by the way do is still need to use string ???
because BufferedReader Only reads as String right? so how can i make the string be character..
:-/

for example iam declaring an integer:

String = input;
int num;

System.out.print("******");
num = Integer.parseInt (input = in.readLine());

how can i make it like a character or should i declare a character directly with out declaring a string???
and
if the array is 20 should i write it this way?

char characterArray[20] = new char[charArraySize];

:icon_question: :confused:
iam kinda lost please help further explanation.

I said

char characterArray[] = new char[charArraySize];

I had said replace "charArraySize" with the appropriate size.
So if your array size is 20 it should be:-

char characterArray[] = new char[20];

And no BufferedReader can also read a character at a time. Refer here for more details on that.

Heres the javadocs of the String class of Java, it provides a function to export the data in a given String to a char array directly, so you wouldn't even have to initialize the char array the way we have discussed anyways.

really many thanks to you.. i wish i could do something for you too
THAnks so much:)

>really many thanks to you.. i wish i could do something for you too

Well, you could mark the thread as "Solved" and leave him a favorable reputation comment on the post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.