Bubble sorting

Reply

Join Date: Aug 2008
Posts: 6
Reputation: qaz1134 is an unknown quantity at this point 
Solved Threads: 0
qaz1134 qaz1134 is offline Offline
Newbie Poster

Bubble sorting

 
0
  #1
Sep 13th, 2008
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:
  1. Exception in thread "main" java.lang.Error: Unresolved compilation problems:
  2. The local variable characterArray may not have been initialized
  3. The local variable characterArray may not have been initialized
  4. The local variable characterArray may not have been initialized
  5.  
  6. 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.


  1. import java.util.Arrays;
  2. import java.io.*;
  3.  
  4. public class BubbleSort {
  5.  
  6. /**
  7. * @param args
  8. */
  9. public static void main(String[] args) throws IOException{
  10. BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
  11. char characterArray[];
  12.  
  13. System.out.print("Enter Name:\t");
  14.  
  15. Arrays.sort(characterArray);
  16.  
  17. for (int i = 0; i < characterArray.length; i++)
  18. {
  19. System.out.print(characterArray[i]);
  20. System.out.print(", ");
  21. }
  22.  
  23.  
  24. }
  25.  
  26. }


can you please help me??
i really need your help.
Thnks so much
Last edited by qaz1134; Sep 13th, 2008 at 1:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Bubble sorting

 
0
  #2
Sep 13th, 2008
You need to initialize your character array something like this:-
  1. 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 ??
Last edited by stephen84s; Sep 13th, 2008 at 2:06 pm.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 6
Reputation: qaz1134 is an unknown quantity at this point 
Solved Threads: 0
qaz1134 qaz1134 is offline Offline
Newbie Poster

Re: Bubble sorting

 
0
  #3
Sep 13th, 2008
Originally Posted by stephen84s View Post
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:
  1. String = input;
  2. int num;
  3.  
  4. System.out.print("******");
  5. 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?
  1. char characterArray[20] = new char[charArraySize];

iam kinda lost please help further explanation.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Bubble sorting

 
0
  #4
Sep 13th, 2008
I said
  1. char characterArray[] = new char[charArraySize];
I had said replace "charArraySize" with the appropriate size.
So if your array size is 20 it should be:-
  1. 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.
Last edited by stephen84s; Sep 13th, 2008 at 2:51 pm.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 6
Reputation: qaz1134 is an unknown quantity at this point 
Solved Threads: 0
qaz1134 qaz1134 is offline Offline
Newbie Poster

Re: Bubble sorting

 
0
  #5
Sep 16th, 2008
really many thanks to you.. i wish i could do something for you too
THAnks so much
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Bubble sorting

 
0
  #6
Sep 16th, 2008
>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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC