• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Can someone fix me?  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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++
}
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the quick response, but could you explain how I could swap the numbers for the sort?
 
Carl Headley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks so so much everybody.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!