I am trying to finalize a project in Java, however I am having a hard time getting the Collections.sort to work and sort my output by natural order. Basically I should be getting:
Total properties listed: 7
Total value of properties listed: 2120000.00
110001
110020
110223
110333
110421
110442
112352
But I am getting:
Total properties listed: 7
Total value of properties listed: $2120000.00
110001
110223
110020
110333
110442
110421
112352
Everything is working except for my natural ordering of the ID numbers listed at the bottom. If anyone can take a look through my code and let me know what I might be doing wrong here any help would be appreciated! Thanks!
package kettask2pt2; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.io.FileReader; import java.io.File; import java.util.ArrayList; import java.util.*; /** * * @author Brandon */ public class KETTask2Pt2{ public static void main(String[] args) throws FileNotFoundException, IOException { // Ask for the file name listings.txt Scanner inputscan = new Scanner(System.in); System.out.print("Please enter listings.txt: "); String inputFile = inputscan.next(); // Creates variables & BufferedWriters String line = null ; PrintWriter out = new PrintWriter("overview.txt"); File input = new File(inputFile); BufferedReader in = new BufferedReader(new FileReader(input)); int count = 0; double sum = 0; // Totals and adds properties listed and the values of the properties while ((line = in.readLine())!= null) { count++; String[] thelist = line.split("[\\s}]"); Double totalvalue = Double.parseDouble(thelist[2]); { sum+=totalvalue; } } out.println("Total properties listed: " + count); out.println("Total value of properties listed: " + "$" +sum + "0" +"\n"); in.close(); //Adds the id numbers for the individual listings try { ArrayList<String> ids = new ArrayList<String>(); Scanner idscan = new Scanner((input)); while (idscan.hasNextLine()) { line = idscan.nextLine(); String[] fields = line.split("[\\s}]"); String idnums = (fields [0]); { Collections.sort(ids); out.println(idnums); in.close(); } } }catch(Exception e){ } //closes the print writer out.flush(); out.close(); } }