Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #16  
Old May 6th, 2008, 10:13 AM
jenl2881 jenl2881 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 9 jenl2881 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Java application using loops and sort


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway