Click here to Skip to main content
15,892,161 members
Home / Discussions / Java
   

Java

 
QuestionDelimiters Pin
Member 107286679-Aug-15 10:58
Member 107286679-Aug-15 10:58 
AnswerRe: Delimiters Pin
Richard MacCutchan10-Aug-15 0:56
mveRichard MacCutchan10-Aug-15 0:56 
QuestionJava Bubble Sort Pin
Frankie12457-Aug-15 5:00
Frankie12457-Aug-15 5:00 
QuestionRe: Java Bubble Sort Pin
Richard MacCutchan7-Aug-15 6:21
mveRichard MacCutchan7-Aug-15 6:21 
AnswerRe: Java Bubble Sort Pin
Frankie12457-Aug-15 6:37
Frankie12457-Aug-15 6:37 
GeneralRe: Java Bubble Sort Pin
Richard MacCutchan7-Aug-15 21:09
mveRichard MacCutchan7-Aug-15 21:09 
GeneralRe: Java Bubble Sort Pin
Frankie12459-Aug-15 9:33
Frankie12459-Aug-15 9:33 
GeneralRe: Java Bubble Sort Pin
Richard MacCutchan10-Aug-15 0:54
mveRichard MacCutchan10-Aug-15 0:54 
A couple of obvious errors first:
Java
array[i] = (int) Math.random()*9;

You are casting the result of Math.random to an integer and multiplying the result by 9. But the return value of Math.random is a double greater than 0.0 and less than 1.0, so the integer result will always be zero. You need to do the multiplication first, thus:
Java
array[i] = (int)(Math.random() * 9);


Java
System.out.println(array);

This statement makes no sense, a primitive type or array has no toString method so you cannot use a statement like this to print its contents, you need to traverse the array and print each value, like:
Java
for (i = 0; i < array.length - 1; i++) {
    System.out.println(array[i]);
}


Now you should be able to see that your sort algorithm will not work. You need to check if the first element is greater than the second before swapping them. You also need a flag to check when no more values are out of order. I would use a while, or do/while loop with a boolean flag that can be set at the beginning, and reset every time an out of order pair is found. Try writing it down in your native language first so you understand the logic, and then write the actual code.
GeneralRe: Java Bubble Sort Pin
Frankie124510-Aug-15 3:22
Frankie124510-Aug-15 3:22 
GeneralRe: Java Bubble Sort Pin
Richard MacCutchan10-Aug-15 3:34
mveRichard MacCutchan10-Aug-15 3:34 
Questioncennect unicenta with a phone problem Pin
Member 116403425-Aug-15 4:56
Member 116403425-Aug-15 4:56 
AnswerRe: cennect unicenta with a phone Pin
Richard MacCutchan5-Aug-15 5:23
mveRichard MacCutchan5-Aug-15 5:23 
QuestionJava Application Help - Inner Class? Pin
Member 1072866731-Jul-15 21:43
Member 1072866731-Jul-15 21:43 
AnswerRe: Java Application Help - Inner Class? Pin
Afzaal Ahmad Zeeshan2-Aug-15 1:55
professionalAfzaal Ahmad Zeeshan2-Aug-15 1:55 
QuestionJAVA SERVLETS and PHP Pin
anaQata31-Jul-15 5:10
professionalanaQata31-Jul-15 5:10 
AnswerRe: JAVA SERVLETS and PHP Pin
Richard MacCutchan1-Aug-15 22:10
mveRichard MacCutchan1-Aug-15 22:10 
GeneralRe: JAVA SERVLETS and PHP Pin
anaQata1-Aug-15 23:21
professionalanaQata1-Aug-15 23:21 
GeneralRe: JAVA SERVLETS and PHP Pin
Richard MacCutchan2-Aug-15 1:12
mveRichard MacCutchan2-Aug-15 1:12 
AnswerRe: JAVA SERVLETS and PHP Pin
Member 119648056-Sep-15 22:58
Member 119648056-Sep-15 22:58 
QuestionJava Pin
jubayer30-Jul-15 0:29
jubayer30-Jul-15 0:29 
AnswerRe: Java Pin
Pete O'Hanlon30-Jul-15 0:45
mvePete O'Hanlon30-Jul-15 0:45 
QuestionJava Set Game Pin
Member 1186286325-Jul-15 6:58
Member 1186286325-Jul-15 6:58 
AnswerRe: Java Set Game Pin
Richard MacCutchan25-Jul-15 8:17
mveRichard MacCutchan25-Jul-15 8:17 
QuestionJava Applet Help Pin
Member 1072866723-Jul-15 0:38
Member 1072866723-Jul-15 0:38 
AnswerRe: Java Applet Help Pin
Richard MacCutchan23-Jul-15 21:12
mveRichard MacCutchan23-Jul-15 21:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.