Hello all,
I am working on a program for school and I am having trouble with sorting my list. The numbers I receive in my output are correct, but not in ascending order as required.
Here is my current code
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package gtt1_task2b; import java.util.Scanner; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.IOException; import java.io.FileReader; import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; /** * */ public class GTT1_Task2B{ public static void main(String[] args) throws FileNotFoundException, IOException { // Prompt for the input and output file names Scanner properties = new Scanner(System.in); System.out.print("Input file: "); String inputFile = properties.next(); // Create variables String id = null ; BufferedWriter pwfo = null; try{ pwfo = new BufferedWriter(new FileWriter("C:\\Users\\zubeda.a.hemani\\Documents\\NetBeansProjects\\GTT1_Task2\\src\\gtt1_task2\\overview.txt")); } catch (IOException e){ } PrintWriter pwo = new PrintWriter(pwfo); File input = new File(inputFile); BufferedReader in = new BufferedReader(new FileReader(input)); int count = 0; double sum = 0; //int i = 0; //ArrayList<Double> plist = new ArrayList<Double>(); while ((id = in.readLine())!= null) { count++; String[] proplist = id.split("[\\s}]"); Double pvalue = Double.parseDouble(proplist[2]); for (int i=0; i < proplist.length; i++) { sum+= pvalue; } } System.out.println("Total properties listed: " + count); System.out.println("Total value of properties listed: " + sum + "\n"); in.close(); try { ArrayList<String> propids = new ArrayList<String>(); Scanner idlist = new Scanner((input)); //print property ids while (idlist.hasNextLine()) { id = idlist.nextLine(); String[] fields =id.split("[\\s}]"); String pids = (fields [0]); { Collections.sort(propids); Iterator j =propids.iterator(); System.out.println(pids); for (int i =0; i<6;i++) in.close(); } } //close Print Writer pwo.flush(); pwo.close(); }catch(Exception e){ } } }
I am not sure where I am going wrong. Any assitance is greatly appreciated.
Thank you.