Hello,
i'm a new member here in this forum..
and i think lot of programmers may help me here..
i'm a newbie also in java programming..

my teacher wants us to make a program that accepts 5 integers and then arrange it from highest to lowest..
i have an idea how to do it but it is on a hard way.. my teacher wants us to do it in a short way.. i dont have much idea in doing it on that way.. (because my logic is POOR..T-T)
but i tried my best i try to figure out last night until now i cant get it.. here's my code..

/**
 * @(#)highest.java
 *
 * highest application
 *
 * @o0sample0o
 * @version 1.00 2009/11/24
 */
 import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

public class highest {
    public static void main(String[] args) {
    JTextArea myTextArea = new JTextArea(5,10);
    JScrollPane myScrollBar = new JScrollPane(myTextArea);
    int number[],  first=0, second=0, third=0,fourth=0,fifth=0;
    
    for( int x=1;x<=5;x++)
    {
    	 
    	 number[x] = Integer.parseInt(JOptionPane.showInputDialog("Enter Number"+x+": "));
    }
    if(number[1]>first)
    {
    	first=number[1];
    }
    else if(number[2]>second)
    {
    	second=number[2];
    }
    else if(number[3]>third)
    {
    	third=number[3];
    }
    else if(number[4]>fourth)
    {
    	fourth=number[4];
    }
    else
    {
    	fifth=number[5];
    }
    myTextArea.setText("The 1st largest is: "
    	+first+"\nThe 2nd largest is: "
    		+second+"\nThe 3rdnd largest is: "
    			+third+"\nThe 4th largest is: "
    				+fourth+"\nThe 5th largest is: "
    					+fifth);
     
    JOptionPane.showMessageDialog(null, myScrollBar);
    System.exit(0);
    }
    }

oh.. and one more question..
i try to use an one dimensional array..
i there anyway to execute it here in java? i have small knowledge in c++.. so i think the syntax here in java have lot different on c++?
thanks for reading my problem..
Thanks DANIWEB!:)

Recommended Answers

@darenn 76, please wrap your codes inside the CODE-tags.

Arrays.sort(arrayName)

will sort the arrayName from lowest to highest.

So to print the numbers from highest to lowest, you need to do a for loop beginning from the arrayName.lengh() - 1 to 0.

Jump to Post

All 4 Replies

for(int i=0; i< (nameOfArray.length-1); ++i)
 {
   for(int j=0; j<(nameOfArray.length-1); ++1)
     {
      if(nameOfArray[j] > nameOfArray{j+1])
         {
             temp=nameOfArray[j];
             nameOfArray[j]=nameOfArray[j+1);
             nameOfArray[j+1]=temp;
          }
      }
 }

Check your java book, it should have this example

Google Arrays.sort.

Just get the user input into an array, and use Arrays.sort( anArray),
then the anArray is sorted.

Maybe this will help you.........

import java.util.Scanner;
public class highestLowest {
public static void main(String[]args) {

Scanner input = new Scanner(System.in);

int highestNum=0;
int lowestNum =5000;
int num, numTimes;

System.out.println("How many numbers would you like to enter? ");
numTimes = input.nextInt();

for (int c=1; c <= numTimes; c++) {
	System.out.println("Please enter an integer !");
	num = input.nextInt();
if (c == 1)	{
lowestNum = num;
highestNum = num;	}
	else	{
if (num < lowestNum)	{
lowestNum = num;	}	
	if (num > highestNum)	{
	highestNum = num;	}}}
System.out.println("The higest number is " + highestNum);
System.out.println("The lowest number is " + lowestNum);
}}

@darenn 76, please wrap your codes inside the CODE-tags.

Arrays.sort(arrayName)

will sort the arrayName from lowest to highest.

So to print the numbers from highest to lowest, you need to do a for loop beginning from the arrayName.lengh() - 1 to 0.

commented: Thanks Eric! for reporting us. +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.