Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Simple program: NEED HELP!!

Rohit Gupta
Greenhorn
Posts: 2
Hi,
I have been learning java for a couple of months...
I have been trying to write a simple program that will print 6 numbers, in ascending order, on the console. I have these numbers in an array:
int num[] = {46, 5, 17, 89, 77, 76}
how do I do this?...
Any help will be appreciated.
Thanks

Steven YaegerII
Ranch Hand
Posts: 182
Do you want to print the numbers in the array, in ascending order, no matter how they are placed in the array? This would be so you can play with logic flow.
Or do you just want to print those numbers in ascending order?

Rohit Gupta
Greenhorn
Posts: 2
Yep, I want to use the logic flow...can you help me with some code?
Thanks.

Eric Edwards
Ranch Hand
Posts: 60
Originally posted by Steven YaegerII:
Do you want to print the numbers in the array, in ascending order, no matter how they are placed in the array? This would be so you can play with logic flow.
Or do you just want to print those numbers in ascending order?

Try this!
import java.util.*;
public class NewSortArray
{
public static void main(String[] args)
{
int[] num = {46,5,17,89,77,76};
Arrays.sort(num);
for (int i = 5; i < num.length; --i)
System.out.println(num[i]);
}
}

Anonymous
Ranch Hand
Posts: 18944
hi!
just try this code:
class example
{
public static void main(String args[])
{
int[] num = {46,5,17,89,77,76};
int temp = 0 ;
for(int i=0;i<num.length;i++)>
{
for(int j=0;j<num.length;j++)>
{
if(num[i] < num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
for(int j=0;j<num.length;j++)>
{
System.out.println(num[j]);
}
}
}

Anonymous
Ranch Hand
Posts: 18944
hi!
just try this code:
class example
{
public static void main(String args[])
{
int[] num = {46,5,17,89,77,76};
int temp = 0 ;
for(int i=0;i<num.length;i++)>
{
for(int j=0;j<num.length;j++)>
{
if(num[i] < num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
for(int j=0;j<num.length;j++)>
{
System.out.println(num[j]);
}
}
}