okay I have it almost complete with one issue: When the report runs it is suppose to add up the property values for each agent and only display the total value of the properties. like:COMMERICAL
FARM
LAND
RESIDENTIAL
101 600000.00
105 30000.00
106 200000.00
107 1040000.00
110 250000.00
but for some reason when I run the report it is adding the property values up but it is also displaying the 1st value of each agent. like:
COMMERCIAL
FARM
LAND
RESIDENTIAL
101:500000.0
101:600000.0
105:30000.0
106:200000.0
107:1000000.0
107:1040000.0
110:250000.0
as can see if you compare to the origional file:
500000.00 101
100000.00 101
1000000.00 107
30000.00 105
200000.00 106
40000.00 107
250000.00 110
it is adding but for some reason it is showing the original agents 1st property and also the amout after the add.
I cannot see what I am missing,
thank you,
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package taska;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
/**
*
* @author chadblack
*/
public class TaskA {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
throws FileNotFoundException
{
//Prompt the user for the name of the input file (listings.txt).
Scanner getfile = new Scanner(System.in);
System.out.print("Input file: ");//this displays for the user
String inputFileName = getfile.next();//this will retrieve the file listings.txt
BufferedWriter addinfo = null;
try{
addinfo = new BufferedWriter(
new FileWriter("agentreport.txt", true));
}
catch(IOException e){
}
try (PrintWriter display = new PrintWriter(addinfo)) {
Set<String> propertyTypes;
propertyTypes = pTypes(inputFileName);
for (String type: propertyTypes)
{
System.out.println(type);
display.println(type);
}
//pwo.flush();
//pwo.close();
//construct agent ids and vales treeSet
Set<String> agentRpt = agentValue(inputFileName);
//print the agents id's
for (String tail: agentRpt)
{
{
System.out.println(tail);
display.println(tail);
}
}
display.flush();
display.close();
}
}
/**
* Reads the Input file
*
* returns the alphabetized property types in correct format
*/
public static Set<String> pTypes (String inputFileName)
throws FileNotFoundException
//now construct treeSet to return the property type
{
Set<String> type = new TreeSet<>();
try (Scanner pt = new Scanner(new File(inputFileName))) {
pt.useDelimiter("[1234567890.]");
while(pt.hasNext())
{
type.add(pt.next().toUpperCase());
}
}
return type;
}
/**
* Reads the file
* returns the agents id's and property values.
*/
public static Set<String> agentValue(String inputFileName)
throws FileNotFoundException
{
TreeSet<String> tail = new TreeSet<>();
SortedMap<String, Number> agentValues = new TreeMap<>();
Scanner in = new Scanner(new File(inputFileName));
String line = inputFileName;
while (in.hasNext())
{
try {
line = in.nextLine();
String[] fields = line.split("[\\s}]");
String agentnumber = (fields [3]);
Double propValue = Double.parseDouble(fields [2]);
if (agentValues.containsKey(agentnumber))
{
propValue = agentValues.get(agentnumber).doubleValue();
}
agentValues.put(agentnumber, propValue);
}catch (Exception e){
}
Set<String> keySet = agentValues.keySet();
for (String key : keySet)
{
Number value = agentValues.get(key);
tail.add(key+ ": "+"$"+ value);
}
}
return tail;
}
}