943,685 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1762
  • Java RSS
Sep 13th, 2008
0

Bubble sorting

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  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.


Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
qaz1134 is offline Offline
6 posts
since Aug 2008
Sep 13th, 2008
0

Re: Bubble sorting

You need to initialize your character array something like this:-
java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Sep 13th, 2008
0

Re: Bubble sorting

Click to Expand / Collapse  Quote originally posted by stephen84s ...
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:
Java Syntax (Toggle Plain Text)
  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?
java Syntax (Toggle Plain Text)
  1. char characterArray[20] = new char[charArraySize];

iam kinda lost please help further explanation.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
qaz1134 is offline Offline
6 posts
since Aug 2008
Sep 13th, 2008
0

Re: Bubble sorting

I said
java Syntax (Toggle Plain Text)
  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:-
java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Sep 16th, 2008
0

Re: Bubble sorting

really many thanks to you.. i wish i could do something for you too
THAnks so much
Reputation Points: 10
Solved Threads: 0
Newbie Poster
qaz1134 is offline Offline
6 posts
since Aug 2008
Sep 16th, 2008
0

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Timing of the word
Next Thread in Java Forum Timeline: Noob and unsure





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC