DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Bubble sorting (http://www.daniweb.com/forums/thread145592.html)

qaz1134 Sep 13th, 2008 1:48 pm
Bubble sorting
 
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

stephen84s Sep 13th, 2008 1:58 pm
Re: Bubble sorting
 
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 ??

qaz1134 Sep 13th, 2008 2:29 pm
Re: Bubble sorting
 
Quote:

Originally Posted by stephen84s (Post 690485)
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.

stephen84s Sep 13th, 2008 2:49 pm
Re: Bubble sorting
 
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.

qaz1134 Sep 16th, 2008 11:35 am
Re: Bubble sorting
 
really many thanks to you.. i wish i could do something for you too
THAnks so much:)

Ezzaral Sep 16th, 2008 11:43 am
Re: Bubble sorting
 
>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.


All times are GMT -4. The time now is 3:48 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC