| | |
Help BubbleSort Array wont run?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 1
Reputation:
Solved Threads: 0
I'm just learning java and this is my first sort program. I can't get it to run. I sure could use some help.
public class SortCharArray
{
public static void main(String[] args) throws Exception
{
char[] someChars = new char[10];
int x;
for(x = 0; x < someChars.length; ++x)
{
System.out.print("Enter a character ");
someChars[x] = (char)System.in.read();
System.in.read(); System.in.read();
}
System.out.println("Before sort");
for(x = 0; x < someChars.length; ++x)
System.out.print(someChars[x] + " ");
bubbleSort(someChars, someChars.length);
System.out.println("\nAfter sort");
for(x = 0; x < someChars.length; ++x)
System.out.print(someChars[x] + " ");
System.out.println();
}
public static void bubbleSort(char[] array, int len)
{
int a, b;
char temp;
int highSubscript = len - 1;
for(a = 0; a < highSubscript; ++a)
{
for(b = 0; b < highSubscript; ++b)
if(array[b] > array[b = 1])
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
}
}
}
public class SortCharArray
{
public static void main(String[] args) throws Exception
{
char[] someChars = new char[10];
int x;
for(x = 0; x < someChars.length; ++x)
{
System.out.print("Enter a character ");
someChars[x] = (char)System.in.read();
System.in.read(); System.in.read();
}
System.out.println("Before sort");
for(x = 0; x < someChars.length; ++x)
System.out.print(someChars[x] + " ");
bubbleSort(someChars, someChars.length);
System.out.println("\nAfter sort");
for(x = 0; x < someChars.length; ++x)
System.out.print(someChars[x] + " ");
System.out.println();
}
public static void bubbleSort(char[] array, int len)
{
int a, b;
char temp;
int highSubscript = len - 1;
for(a = 0; a < highSubscript; ++a)
{
for(b = 0; b < highSubscript; ++b)
if(array[b] > array[b = 1])
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
}
}
}
Well, supplying the errors you are getting would be helpful.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Please use code tags when posting code. Read the information in the links below.
This is how I code it in c/c++:
This is how I code it in c/c++:
Java Syntax (Toggle Plain Text)
for(a = 0; a < highSubscript-1; ++a) { for(b = 0; b < highSubscript; ++b) { if(array[a] > array[b]) { temp = array[a]; array[a] = array[b]; array[b] = temp; } } }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Pascal : Cant see why program wont run, exits with error. (Pascal and Delphi)
- Repost of Bubblesort error, wont debug (C++)
- Dell wont run (Troubleshooting Dead Machines)
- Script wont run on different computer (Python)
- my G4 sawtooth 400 wont run dual monitors, at the same time (Apple Hardware)
- Run-time Error when printing Array Contents. (C)
Other Threads in the Java Forum
- Previous Thread: need some help
- Next Thread: Fibonacci Recursion - Illegal Start of expression!
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing threads time tree unlimited utility webservices windows






