
May 6th, 2008, 10:13 AM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 9
Time spent in forums: 1 h 3 m 53 sec
Reputation Power: 0
|
|
|
Code
This is what I have. I know it's a long code, probably could be shorter, but this is what I have and I think it's right.
Code:
import javax.swing.JOptionPane;
public class Final
{
public static void main(String[] args)
{
String N = JOptionPane.showInputDialog("How many names do you wish to enter into the system?");
int Names = Integer.parseInt(N);
String [][] arrays = new String [Names][11];
for(int i=0; i<=Names-1; i++)
{
String input = JOptionPane.showInputDialog("Please Enter First Name.");
arrays [i][0] = input;
String input1 = JOptionPane.showInputDialog("Please Enter Last Name.");
arrays [i][1] = input1;
String input2 = JOptionPane.showInputDialog("Please Enter Street Address.");
arrays [i][2] = input2;
String input3 = JOptionPane.showInputDialog("Please Enter City.");
arrays [i][3] = input3;
String input4 = JOptionPane.showInputDialog("Please Enter 5-digit Zip Code.");
arrays [i][4] = input4;
String input5 = JOptionPane.showInputDialog("Please Enter State. (ex. TX)");
arrays [i][5] = input5;
String input6 = JOptionPane.showInputDialog("Please Enter 2-digit Day Of Birth.");
arrays [i][6] = input6;
String input7 = JOptionPane.showInputDialog("Please Enter 2-digit Month Of Birth.");
arrays [i][7] = input7;
String input8 = JOptionPane.showInputDialog("Please Enter 4-digit Year Of Birth.");
arrays [i][8] = input8;
String input9 = JOptionPane.showInputDialog("Please Enter Title. (ex. Mr.)");
arrays [i][9] = input9;
String input10 = JOptionPane.showInputDialog("Please Enter Yearly Salary");
arrays [i][10] = input10;
}
String sortad = JOptionPane.showInputDialog("Sort By? " + "X. Ascending, Y. Descending");
//ASCENDING
if(sortad.equalsIgnoreCase("x"))
{
String sort = JOptionPane.showInputDialog("Sort By? " + "A. First Name, B. Last Name, C. Zip code, D. Title, E. Salary, or F. Age");
//FIRST NAME
if(sort.equalsIgnoreCase("a"))
for (int x=0; x<Names; x++)
{
for (int y = x+1; y < Names; y++)
{
if(arrays[y][0].compareTo(arrays[y][0])>0)
{
for(int z = 0; z < 11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
//LAST NAME
if ( sort.equalsIgnoreCase("b") )
for (int x =0; x<Names; x++)
{
for (int y = x+1; y<Names; y++)
{
if(arrays[x][1].compareTo(arrays[y][1])>0)
{
for(int z=0; z<11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
//ZIP CODE
if ( sort.equalsIgnoreCase("c") )
for (int x=0; x<Names; x++)
{
for (int y=x+1; y<Names; y++)
{
if(arrays[x][4].compareTo(arrays[y][4])>0)
{
for(int z=0; z<11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
//TITLE
if ( sort.equalsIgnoreCase("d") )
for (int x=0; x<Names; x++)
{
for (int y=x+1; y<Names; y++)
{
if(arrays[x][9].compareTo(arrays[y][9])>0)
{
for(int z=0; z<11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
//SALARY
if ( sort.equalsIgnoreCase("e") )
for (int x=0; x<Names; x++)
{
for (int y=x+1; y<Names; y++)
{
if(arrays[x][10].compareTo(arrays[y][10])>0)
{
for(int z = 0; z < 11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
//AGE
if ( sort.equalsIgnoreCase("f") )
for (int x=0; x<Names; x++)
{
for (int y=x+1; y<Names; y++)
{
if(arrays[x][8].compareTo(arrays[y][8])<0)
{
for(int z = 0; z < 11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
for (int i=0; i<Names; i++)
{
for(int s=0; s<11; s++)
{
System.out.print(arrays[i][s] + " ");
}
System.out.println();
}
}
//DESCENDING
if(sortad.equalsIgnoreCase("y"))
{
String sort = JOptionPane.showInputDialog("Sort By? " + "A. First Name, B. Last Name, C. Zip code, D. Title, E. Salary, or F. Age");
//FIRST NAME
if(sort.equalsIgnoreCase("a"))
for (int x=0; x<Names; x++)
{
for (int y = x+1; y < Names; y++)
{
if(arrays[y][0].compareTo(arrays[y][0])<0)
{
for(int z = 0; z < 11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
//LAST NAME
if (sort.equalsIgnoreCase("b") )
for (int x =0; x<Names; x++)
{
for (int y = x+1; y<Names; y++)
{
if(arrays[x][1].compareTo(arrays[y][1])<0)
{
for(int z=0; z<11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
//ZIP CODE
if ( sort.equalsIgnoreCase("c") )
for (int x=0; x<Names; x++)
{
for (int y=x+1; y<Names; y++)
{
if(arrays[x][4].compareTo(arrays[y][4])<0)
{
for(int z=0; z<11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
//TITLE
if ( sort.equalsIgnoreCase("d") )
for (int x=0; x<Names; x++)
{
for (int y=x+1; y<Names; y++)
{
if(arrays[x][9].compareTo(arrays[y][9])<0)
{
for(int z=0; z<11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
//SALARY
if ( sort.equalsIgnoreCase("e") )
for (int x=0; x<Names; x++)
{
for (int y=x+1; y<Names; y++)
{
if(arrays[x][10].compareTo(arrays[y][10])<0)
{
for(int z = 0; z < 11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
//AGE
if ( sort.equalsIgnoreCase("f") )
for (int x=0; x<Names; x++)
{
for (int y=x+1; y<Names; y++)
{
if(arrays[x][8].compareTo(arrays[y][8])>0)
{
for(int z = 0; z < 11; z++)
{
String names = arrays[x][z];
arrays[x][z] = arrays[y][z];
arrays[y][z]= names;
}
}
}
}
for (int i=0; i<Names; i++)
{
for(int s=0; s<11; s++)
{
System.out.print(arrays[i][s] + " ");
}
System.out.println();
}
}
}
}
Thanks! 
|