0

I'm looking to sort the contacts read from a file in alphabetical order by last name to the console? How would I go about doing so? The contacts are already written to file starting with the last name, I just want to read them back into the application in alphabetical order when a user wants to view the contacts in the console.

// Read from file, print to console. by XXXXX
            // ----------------------------------------------------------
            int counter = 0;
            String line = null;

            // Location of file to read
            File file = new File("contactlist.csv");

            try {

                Scanner scanner = new Scanner(file);

                while (scanner.hasNextLine()) {
                    line = scanner.nextLine();
                    System.out.println(line);
                    counter++;
                }
                scanner.close();
            } catch (FileNotFoundException e) {

            }
            System.out.println("\n" + counter + " contacts in records.");

        }
        break;
        // ----------------------------------------------------------
        // End read file to console. by XXXX

3 Answers3

2

Before printing, add each line to a sorted set, as a TreeSet:

Set<String> lines = new TreeSet<>();
while (scanner.hasNextLine()) {
    line = scanner.nextLine();
    lines.add(line);
    counter++;
}

for (String fileLine : lines) {
    System.out.println(fileLine);
}
Igor Rodriguez
  • 1,196
  • 11
  • 16
  • Tried this out. However, it says I have a "Duplicate local variable line" and wont run. If I rename the string I get a normal output not in alphabetical order. – user2149907 Mar 10 '13 at 20:25
  • @user2149907: The set has "lines" as name, while the string has "line". Haven't you a typo? – Igor Rodriguez Mar 10 '13 at 20:28
  • "String line = null;" is causing a duplicate. – user2149907 Mar 10 '13 at 20:37
  • @user2149907: I did put the name for the String in the for loop equal to the name of your previous variable. – Igor Rodriguez Mar 10 '13 at 20:45
  • Worked like a charm. However, is their a simple adjust to ignore the case? such as, Adams, adams? It will print Brown before lowercase adams. But, will print uppercase Adams before Brown as it should. But, once again. Thank you. It worked great. – user2149907 Mar 10 '13 at 20:49
  • @user2149907: The output is the same as the file contents. If you need to adjust the case for the first letter of each line, try this: http://stackoverflow.com/questions/5725892/how-to-capitalize-the-first-letter-of-word-in-a-string-using-java – Igor Rodriguez Mar 10 '13 at 20:57
  • Thanks! @igor Rodriguez that link help very much. My problem has been solved 100% – user2149907 Mar 10 '13 at 21:01
1
package main.java.com.example;

import java.io.*;
import java.net.URL;
import java.util.Set;
import java.util.TreeSet;

public class ReadFromCSV {
    public static void main(String[] args) {
        try {
            final ClassLoader loader = ReadFromCSV.class.getClassLoader();
            URL url = loader.getResource("csv/contacts.csv");
            if (null != url) {
                File f = new File(url.getPath());
                BufferedReader br = new BufferedReader(new FileReader(f));
                Set<String> set = new TreeSet<String>();
                String str;

                while ((str = br.readLine()) != null) {
                    set.add(str);
                }

                for (String key : set) {
                    System.out.println(key);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
gashu
  • 484
  • 6
  • 17
0

Read names from the file, put them in an object of class SortedSet.

A human being
  • 1,220
  • 8
  • 18