Hi guys (again), I have a new issue.
I am trying to bubble sort my array of objects to display in alphabetical order.
Here's the code:
Subscriber class:
package assignmentone;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Random;
/**
*
* @author Bill
*/
public class Subscriber {
//Field declarations
private String name;
private String url;
private String friendsList;
public int friendIndex = 0;
private int adClicks;
//Default Constructor
Subscriber()
{
}
//Set Constructor Fields
Subscriber(String subName, String subUrl, String subFriends,
int subClicks)
{
name = subName;
url = subUrl;
friendsList = subFriends;
adClicks = subClicks;
}
//=============Accessor methods=============
public String getName()
{
return name;
}
public String getUrl()
{
return url;
}
public String getFriends()
{
return friendsList;
}
public int getClicks()
{
return adClicks;
}
//=============Mutator methods=============
public void setName(String subName)
{
name = subName.toUpperCase();
}
public void setUrl(String subUrl)
{
url = subUrl.toLowerCase();
}
public void setFriends(String subFriends)
{
friendsList = subFriends.toUpperCase();
}
public void setClicks(int subClicks)
{
adClicks = subClicks;
}
//=============ReadIn method=============
public void readIn()
{
String anyMore;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter subscriber name: ");
name = keyboard.nextLine().toUpperCase();
System.out.println("Enter subscriber URL: ");
url = keyboard.nextLine().toUpperCase();
do
{
String friendName;
System.out.println("Name of friend (last name first!): ");
friendName = keyboard.nextLine().toUpperCase();
friendsList = friendsList.concat(friendName);
System.out.println("\nDo you want to enter another name? "
+ "(Y/N): ");
anyMore = keyboard.nextLine().toUpperCase();
friendIndex++;
if (anyMore.equals("Y"))
{
friendsList = friendsList.concat(";");
System.out.println();
}
}while(anyMore.equals("Y"));
}
//=============Display method=============
public void display()
{
StringTokenizer tokenizedList;
Random generator = new Random();
adClicks = generator.nextInt(10000);
System.out.println("\nSubscriber: " + name);
System.out.println("URL: " + url);
tokenizedList = new StringTokenizer(friendsList, ";");
System.out.println("\n" + adClicks + " users have clicked on your ads.");
System.out.println("\nYou have " + friendIndex +" friend(s): ");
while (tokenizedList.hasMoreTokens())
{
System.out.println(tokenizedList.nextToken());
}
}
//=============Find Friend method=============
public boolean isAmongFriends(String aFriend)
{
boolean isInList = false;
StringTokenizer tokenizedStr = new StringTokenizer(friendsList, ";");
System.out.println("Friend's name: ");
String extractFriend;
while(tokenizedStr.hasMoreTokens() && !isInList)
{
extractFriend = tokenizedStr.nextToken();
if(extractFriend.equals(aFriend))
{
System.out.println(extractFriend +" is among your friends.");
isInList = true;
}
}
return true;
}
//=============Bubble sort method=============
public static void BubbleSort(String[] input)
{
int j;
boolean flag = true;
String temp = null;
while(flag)
{
flag = false;
for(j = 0; j < input.length - 1; j++)
{
if(input[j].compareTo(input[j+1]) > 0)
{
temp = input[j];
input[j] = input[j+1];
input[j+1] = temp;
flag = true;
}
}
}
}
}
Main:
package assignmentone;
import java.util.Scanner;
public class AssignmentOne {
//Constant declaration
public static int MAX_NUM = 10;
public static void main(String[] args) {
Subscriber subList[] = new Subscriber[MAX_NUM];
String anyMoreSub;
String menuChoice;
String askQuit;
int subIndex = 0;
boolean isDone = false;
Scanner keyboard = new Scanner(System.in);
for(int i = 0; i < MAX_NUM ;i++)
{
subList[i] = new Subscriber("", "", "", 0);
}
for(int i = 0; i < MAX_NUM ;i++)
{
subList[i].readIn();
System.out.println("\nDo you want to enter another subscriber? "
+ "(Y/N): ");
anyMoreSub = keyboard.nextLine().toUpperCase();
subIndex++;
if (anyMoreSub.equals("N"))
{
break;
}
}
for(int i = 0; i < MAX_NUM ;i++)
{
subList[i].BubbleSort();
}
while(!isDone)
{
System.out.println("\nPlease enter the letter for "
+ "your selection: " +
"\n A - Display all data" +
"\n B - Display data of specific Subscriber" +
"\n C - Display names of Subscribers with specific "
+ "friend" +
"\n D - Quit");
menuChoice = keyboard.nextLine().toUpperCase();
while(!menuChoice.equals("A") && !menuChoice.equals("B") &&
!menuChoice.equals("C") && !menuChoice.equals("D"))
{
System.out.println("\nSorry, you must choose"
+ " one of the valid menu choices. Try again.");
System.out.println("\nPlease enter the number for "
+ "your selection: " +
"\n A - Display all data" +
"\n B - Display data of specific Subscriber" +
"\n C - Display names of Subscribers with specific "
+ "friend" +
"\n D - Quit");
menuChoice = keyboard.nextLine().toUpperCase();
}
if(menuChoice.equals("A"))
{
for(int i = 0; i < subIndex ;i++)
{
subList[i].display();
}
}
if(menuChoice.equals("B"))
{
}
if(menuChoice.equals("C"))
{
}
if(menuChoice.equals("D"))
{
System.out.println("Are you sure you want to exit? (Y/N): ");
askQuit = keyboard.nextLine().toUpperCase();
if(askQuit.equals("Y"))
{
isDone = true;
}
}
}
}
}
As you can see I have the bubble sort method's skeleton in the Subscriber class, but I just can't seem to figure out how to apply it to my object array. Any suggestions would be great!
Thank you!