Sorter

This program takes 10 numbers via a text field and then sorts them in order. Try to rewrite it so that the user can enter the number of integers in the list before entering the list for sorting.

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class SortList extends Applet implements ActionListener
{

int count,count2,temp, length, i=0;
int[] list;
TextField numberField;
public void init()
{

list = new int[10];
numberField = new TextField(3);
add(numberField);
numberField.addActionListener(this);

}
public void paint (Graphics g)
{

//sort numbers and list them
sort();
for (count=0;count<10;count++)
{

g.drawString(""+list[count], 20,(count*20+20));

}

}
public void sort ()
{

//simple bubblesort
for (count=0;count<10;count++)
{

for (count2=0;count2<9; count2++)
{

if (list[count2]>list[count2+1])
{

temp=list[count2+1];
list[count2+1]=list[count2];
list[count2]=temp;

}

}

}

}
public void actionPerformed (ActionEvent e)
{

//take numbers from the texfield and incement the counter by 1
list[i]= Integer.parseInt(numberField.getText());
i=i+1;
//When all 10 numbers are in repaint
if (i>9)

repaint();

}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License