I need help with my sort. it will sort first name but when it gets to last name and DOB it wont. Please help me I tried bubble sort and it didnt work. I would like to use bubble sort.
import javax.swing.*;
import java.util.Scanner;
/**
* @(#)program 2.java
*
*
* @author ********
* @version 1.00 2010/9/28
*/
public class program2{
public static void main( String[] args ){
Scanner input = new Scanner(System.in);
int num; //This will store the number of people that the user wants to enter.
String[][] people; //Setting the array.
String inputString, menu_choice = "0";
System.out.print("How many people would you like to enter?");
num = input.nextInt();
people = new String[num][3];
//The rows in the array will signify the person number while column 0, holds the first name, column
//1 holds the last name, and column 2 hold the birth date.
inputString = input.nextLine();//Consume remaining new line
//for loop below will get asking the question depending on how many people they have put in.
for(int i = 0; i < num; i++)
{
System.out.print("What is person number " + (i + 1) + "'s fisrt name? "); // i +1 will display 1,2,3,etc of the name they need to enter.
people[i][0] = input.nextLine();
System.out.print("What is the person last name? ");
people[i][1] = input.nextLine();
System.out.print("Enter the person birth date in following order(MM/DD/YYYY)? ");
people[i][2] = input.nextLine();
}
int option;
//The while loop below will show the menu so the user can choose what to do next.
while(!menu_choice.equals("3"))
{
System.out.print("Please select a option below:" + "\n Enter 1 to Display" + "\n Enter 2 for to search directory"
+ "\n Press 3 to Exit the program.");// option's to choose what to do.
menu_choice = input.nextLine();
if(menu_choice.equals("1"))
{ //this will be used to display the user names or DOB.
System.out.print("Select how you would like the sort to display :" + "\n 1 to display frist name."
+ "\n 2 to display last name." + "\n 3 to diplay DOB");
option = input.nextInt();
SortArray(option, people, num);
}
else if(menu_choice.equals("2"))
{ //Searrch for a cretain person by first, last, or DOB
System.out.print("How would you like to serach: " +"\n 1 to serach by frist name."
+ "\n 2 to serach by last name." + "\n 3 to serach by DOB");
option = input.nextInt();
SearchArray(option, people, num);
}
else if(menu_choice.equals("3"))// if input is 3 the program will exit
{
System.out.print("Thank you for using the program :)");
}
else
{
System.out.println("The data you putin is not vaild.");
}
}
System.exit(0);
}
public static void SortArray(int choice, String person[][], int num)
{
String[][] t, p;
t = new String[1][3];// t and p is used as a temp storage.
p = new String[1][3];
String s;
String d;//sorts thhe data
boolean sort = true;
int x, y;
int z;
while(sort)//sort using the while loop.
{
for(x = 0; x < num -1; x++)
{
for(y = 0,z=0; y < 3; y++,z++)
{
if((y + 1) != num)
{
t[0][z] = person[z][y];
p[0][z] = person[z][y];
}
}
s = t[0][choice - 1];
System.out.println (s);
d = p[0][choice - 1];
System.out.println(d);
if(s.compareToIgnoreCase(d) > 0 && (x + 1) != num)
{
for(x = 0; x<num; x++)
{
person[x][y] = p[0][y];
person[x+1][y] = t[0][y];
}
}
else
{
sort = false;
}
}
}
for(x = 0; x < num; x++)
{
System.out.println(person[x][0] + " " + person[x][1] + "\t" + person[x][2]);
}
}
public static void SearchArray(int choice, String person[][], int num)
{
Scanner input = new Scanner(System.in);
String inputString = "a";
if(choice == 1 || choice == 2)
{
System.out.print("Which name would you like to serach for? ");// will ask what name to serach for.
inputString = input.nextLine();
}
else if(choice == 3)
{ // if they want to seerach by DOB
System.out.print("Enter the DOB(MM/DD/YYYY)");
inputString = input.nextLine();
}
boolean valid = true;
if(valid)//Check that the user selected the correct option.
{
for(int i = 0; i < num; i++)
{
if(inputString.equalsIgnoreCase(person[i][choice - 1]))//When a match is found, it is printed
{
System.out.println(person[i][0] + " " + person[i][1] + "\t" + person[i][2]);
}
}
}
}
}