Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Question about writing a phonebook sorting program

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Question about writing a phonebook sorting program

    Hello, I have a couple of questions i'd like to ask regarding a recent assignment ive been given to do. I have to write a program that reads in a list of names & numbers in the format:

    Bill Smith 3456
    Jill Sixpack 7654578
    Fred Flintstone 835647
    Donald Duck 298465475
    Micky Mouse 0874567878
    Minnie Mouse 0874567878
    Albert Einstein 9032831

    and print them in the format(Alphabetical surname order, followed by forename then number):

    Duck, Donald 298465475
    Einstein, Albert 9032831
    Flintstone, Fred 835647
    Mouse, Micky 0874567878
    Mouse, Minnie 0874567878
    Sixpack, Jill 7654578
    Smith, Bill 3456

    It is required that we use our own selection sort method to sort the surnames in alphabetical order but what im wondering, is how will I be able to sort them whilst keeping the forename & number associated with the specific surname. For instance, if i read both the surnames & forenames at once and stored them in one array, and used my sort method to sort that alphabetically, it'd sort the forenames aswell. Whereas I only want the surnames to be sorted. Will I need more than one array? Can anyone give me a bit of guidance as to how to approach this? Many thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Question about writing a phonebook sorting program

    Think Classes and Objects . In other words, sort a List of objects containing the appropriate data values.

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Question about writing a phonebook sorting program

    Thanks for the reply copeg I have everything done now and running, but my last question is about the printing format. If i enter a number that starts with a zero, for example 0871234567, it will print 871234567. How can i get it to print a leading zero, if needed. My print statement is as follows:

    System.out.printf("%-25s%-15d\n",surname+", "+forename, number);

Similar Threads

  1. Digital Watch program and Sorting arrays
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 5th, 2011, 05:21 PM
  2. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM
  3. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM
  4. [SOLVED] How to narrow down the range of input in java?
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2009, 11:38 AM
  5. Create Phonebook using inheritance
    By islandGirl in forum Object Oriented Programming
    Replies: 5
    Last Post: February 17th, 2009, 04:42 AM

Tags for this Thread