
print("code sample");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
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\\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]);
for (int i =0; i<propids.size();i++)
propids.add(fields[0]);
{
Collections.sort(propids);
Iterator i =propids.iterator();
System.out.println(pids);
in.close();
}
}
//close Print Writer
pwo.flush();
pwo.close();
}catch(Exception e){
}
}
}
I am really new at Java...This is only the second program I am writing for school and this site was very helpful in pointing out a key error I had in my first program, so I am hopeful you will be able to help me now.
Thank you in advance for any assistance!!!!