Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

Threaded View

  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.


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