-
need help with sorting an string array alphabetically
I can't seem to get this program to sort. Not sure why. Can anyone see a problem with my bubble sort?
thanks.
Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Assignment8b extends Applet implements ActionListener
{
private Button add, display, sort;
private TextField input;
private int j;
private String [] stringArray = new String [10];
private boolean clickedYet = false;
private boolean displayNow = false;
public void init()
{
//add button
add = new Button ("Add");
add(add);
add.addActionListener(this);
//display button
display = new Button ("Display");
add(display);
display.addActionListener(this);
//search button
sort = new Button ("Sort");
add(sort);
sort.addActionListener(this);
//input textField
input = new TextField (20);
add (input);
input.addActionListener (this);
}
public void paint(Graphics g){
g.drawString ("Array output below",50,75);
g.drawLine (50,80,150,80);
if (displayNow)
display (stringArray, g);
}
public void actionPerformed (ActionEvent event)
{
clickedYet = true;
if (clickedYet){
if (event.getSource() == add)
addData (stringArray);
if (event.getSource() == display)
displayNow = true;
if (event.getSource() == sort)
sort (stringArray);
}
input.setText(null);
repaint ();
}
// This section below contains the methods to carry out the tasks 1,2 and 3
public void addData (String [] array)
{ array[j++] = input.getText(); }
public void display(String [] array, Graphics g)
{
int yValue=110;
for (int w=0; w < array.length; w++)
{ g.drawString("Index "+w+" is "+array[w], 70, yValue);
yValue +=20; }
}
//bubble sort
public void sort(String [] array)
{
String temp;
for (int pass=0; pass < (array.length - 1); pass++)
for (int i=1; i < (array.length - pass); i++)
if (array[i].compareTo(array[i-1]) > 0)
{
temp = array [i];
array [i] = array [i - 1];
array [i-1] = temp;
}
}
} // end of program
jbowl to the nineth degree
-
Working code
Here is the code with 2 fixes:
1 - You have initiated the array to size 10, and the last 5 celld where null.
2 - You have assumed the srting will return an algebra compareTo result.
Shrabov.
package mypackage1;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Assignment8b extends Applet implements ActionListener
{
private Button add, display, sort;
private TextField input;
private int j;
private String [] stringArray = new String [5];
private boolean clickedYet = false;
private boolean displayNow = false;
public void init()
{
//add button
add = new Button ("Add");
add(add);
add.addActionListener(this);
//display button
display = new Button ("Display");
add(display);
display.addActionListener(this);
//search button
sort = new Button ("Sort");
add(sort);
sort.addActionListener(this);
//input textField
input = new TextField (20);
add (input);
input.addActionListener (this);
}
public void paint(Graphics g){
g.drawString ("Array output below",50,75);
g.drawLine (50,80,150,80);
if (displayNow)
display (stringArray, g);
}
public void actionPerformed (ActionEvent event)
{
clickedYet = true;
if (clickedYet){
if (event.getSource() == add)
addData (stringArray);
if (event.getSource() == display)
displayNow = true;
if (event.getSource() == sort)
sort (stringArray);
}
input.setText(null);
repaint ();
}
// This section below contains the methods to carry out the tasks 1,2 and 3
public void addData (String [] array)
{ array[j++] = input.getText(); }
public void display(String [] array, Graphics g)
{
int yValue=110;
for (int w=0; w < array.length; w++)
{ g.drawString("Index "+w+" is "+array[w], 70, yValue);
yValue +=20; }
}
//bubble sort
public void sort(String [] array)
{
Integer temp;
Integer[] iArray = new Integer[5];
for (int i = 0; i < array.length ; i++)
{
iArray[i] = Integer.valueOf(array[i]);
}
for (int pass=0; pass < (array.length - 1); pass++)
{
for (int i=1; i < (array.length - pass); i++)
{
if (iArray[i].compareTo(iArray[i-1]) > 0)
{
temp = iArray[i];
iArray[i] = iArray[i-1];
iArray[i-1] = temp;
}
}
}
for (int i = 0; i < array.length ; i++)
{
array[i] = iArray[i].toString();
}
}
} // end of program
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks