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 6 of 6

Thread: Selection Sorting a text array

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Selection Sorting a text array

    I have to use selection sort to sort out a list of names and respective ages from a notepad file.
    I have been able to implement the names and ages into array, and sort the names, but not the ages if there is a plural of the name and put a comma in between.
    Please can you help me - my code is below.
    import java.io.*;
    import java.util.*;
    public class MultipleKeySorting {
        public static void main(String[] args) throws IOException {
            FileInputStream in = new FileInputStream("names_Age.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            int maxIndx = -1;
            String a[] = new String[12];
            for (int h = 0; h < a.length-1; h++) {
                maxIndx++;
                a[maxIndx] = br.readLine();
                System.out.println(a[maxIndx]);
            }
            sort(a);
            System.out.println("");
            print(a);
        }
        static String[] sort(String[] a) {
            String min;
            int minIndex;
            for (int i = 0; i < a.length; ++i) {
                min = a[i];
                minIndex = i;
                for (int j = i + 1; j < a.length-1; j++) { //Find minimum
                    if (min.charAt(0) > a[j].charAt(0)) {
                        min = a[j];
                        minIndex = j;
                    }
                }
                a[minIndex] = a[i]; //swap
                a[i] = min;
            }
            return a;
        } 
        public static void print(String[] a) {
            // prints the elements of the specified array in sequence.
            if (a == null || a.length == 0) return;
            for (int i = 0; i < a.length-1; i++)
                System.out.println(a[i] + ", ");
        }
    }
    Last edited by ComputerSaysNo; January 30th, 2011 at 09:05 AM.


  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: Selection Sorting a text array

    Without knowing the contents of the file its hard to say what is going on.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Selection Sorting a text array

    The data in the notepad is:
    Jones 14
    Abrams 15
    Smith 19
    Jones 9
    Alexander 22
    Smith 20
    Smith 17
    Tippurt 42
    Jones 2
    Herkman 12
    Jones 11

    The output should be:
    Abrams, 15
    Alexander, 22
    Herkman, 12
    Jones, 2
    Jones, 9
    Jones, 11
    Jones, 14
    Smith, 17
    Smith, 19
    Smith, 20
    Tippurt, 42

  4. #4
    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: Selection Sorting a text array

    Check the size of your array versus the number of input lines/names, if the input lines is less than the size of the array the last few elements of the array will never be set (eg their value will be null).

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Selection Sorting a text array

    Thanks, but I think that my array size is fine - it's just that I don't know how to sort the ages out and put the comma in between the name and age of each line.

  6. #6
    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: Selection Sorting a text array

    Quote Originally Posted by ComputerSaysNo View Post
    Thanks, but I think that my array size is fine - it's just that I don't know how to sort the ages out and put the comma in between the name and age of each line.
    Double check...I only count 11 entries in the file, yet you allocate an array size of 12.

Similar Threads

  1. [SOLVED] Sorting an Array
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2010, 11:07 AM
  2. JComboBox selection not showing item from object array
    By oper125 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 24th, 2010, 06:31 AM
  3. array sorting
    By herbisey in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 27th, 2010, 12:07 PM
  4. Selection Sorting of Data type (Char)
    By chronoz13 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:28 PM
  5. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM

Tags for this Thread