File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can someone fix me? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone fix me?" Watch "Can someone fix me?" New topic
Author

Can someone fix me?

Carl Headley
Greenhorn

Joined: Aug 24, 2004
Posts: 3
Below is my code. What I've been trying to do for the past six days is design a loop that reads five integers, sorts them using a while loop, and print out the smallest number entered. I can easily accomplish this with the for loop, but have no clue as to how this is done using a while loop. Can someoone please fix me?



import java.text.*; //this package supports number formatting class
import java.util.Arrays; //this package supports Arrays


public class Whileloop
{
public static void main(String[] args)
{

int[] number = new int[5];

int index=0;
int sum;
System.out.println("Enter 5 whole numbers: ");

sum = 0;
do
{

number [index] = SavitchIn.readLineInt( );
index=index+1;
sum=sum+1;
}while (sum < 5);
/*This part sorts*/

while (number[0]<number[1]&&number[2]&&number[3]&&number[4]);
{

do{
if (number[0]!<number[1]&&number[2]&&number[3]&&number[4])
System.out.print("searching.");
else {System.out.print("the smallest number is"+number[0]);}
} while (number[1]<number[0]&&number[2]&&number[3]&&number[4]);

do{if (number[1]!<number[0]&&number[2]&&number[3]&&number[4]);
System.out.print("...");
else System.out.print("the smallest number is"+number[1]);
} while (number[2]<number[0]&&number[1]&&number[3]&&number[4]);

do{if (number[2]!<number[1]&&number[0]&&number[3]&&number[4]);
System.out.print("...");
else System.out.print("the smallest number is"+number[2]);

} while (number[3]<number[0]&&number[1]&&number[2]&&number[4]);

do{if (number[3]!<number[1]&&number[2]&&number[0]&&number[4]);
System.out.print("...");
else System.out.print("the smallest number is"+number[3]);

} while (number[4]<number[0]&&number[1]&&number[3]&&number[2]);

do{if (number[4]!<number[1]&&number[2]&&number[3]&&number[0]);
System.out.print("...");
else System.out.print("the smallest number is"+number[4]);


}
System.out.print(/n"Thank"+number[0]);


}}


Any corners that can be cut in the while loops would be greatly appreciated.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I can easily accomplish this with the for loop, but have no clue as to how this is done using a while loop.


They're basically the same

for(int x = 0; x < 10 ; x++)
{
//do something
}

int x = 0;
while(x < 10)
{
//do something
x++
}
Lisa D'Aniello
Ranch Hand

Joined: Sep 25, 2003
Posts: 42
Hi Carl,

Holy while loops! To cut down on the loops, you could use 2 loops instead of 5.

You will need 2 temporary variables, 2 counters. Here is the example from my textbook that helped me a lot when I was first learning C++ (slightly modified to fit java syntax)

Carl Headley
Greenhorn

Joined: Aug 24, 2004
Posts: 3
thanks for the quick response, but could you explain how I could swap the numbers for the sort?
Carl Headley
Greenhorn

Joined: Aug 24, 2004
Posts: 3
thanks so so much everybody.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone fix me?