Hello All,
I am working on a program that requries me to read and analyze a .txt file and output the results to a .txt file, but I am getting incorrect output. I have been successfull with part of the program in that it reads the text file and analyzes it, however I need it to take the key(which is Agent ID) and add together the total of the value(which is pValue), however the output just shows each Agent ID and then each pValue. I do not know how to take all the AgentID's and add their pValues together to output the total. I also need the pValues to be in decimal format like(#.##), but I can't seem to get that to work either. Can anyone offer some suggestions?
I thank you in advance for any help.
Here is the code I have come up with so far:
package gtt1_task2; 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 zubeda.a.hemani */ //This program reads a text file, analyzes property listings and outputs a report to a text file public class GTT1_Task2 { /** * @param args the command line arguments */ public static void main(String[] args)throws FileNotFoundException { // Prompts the user for the input file name (listings.txt) by creting a scanner object Scanner listings = new Scanner(System.in); //Ask user for file name System.out.print("Input File: "); String inputFileName = listings.next(); //Use unbuffered file to open text file(listings.txt) and read in property listings. BufferedWriter pwfo = null; try{ pwfo = new BufferedWriter(new FileWriter("C:\\Users\\zubeda.a.hemani\\Documents\\NetBeansProjects\\GTT1_Task2\\src\\gtt1_task2\\listings.txt", true)); } catch (IOException e){ } PrintWriter pwo = new PrintWriter(pwfo); //Construct property type treeSet Set<String> propertyTypes = pTypes(inputFileName); //Print property types from treeSet for(String type: propertyTypes) { System.out.println(type); pwo.println(type); } //pwo.flush(); //pwo.close(); //Construct agent id and value treeSet Set<String> agentRpt = agentValue(inputFileName); //Print agent id and values from key set for(String tail: agentRpt) { { System.out.println(tail); pwo.println(tail); } } pwo.flush(); pwo.close(); } /** Reads the input file. @param inputFileNamegre @return the alphabetized property types in uppercase. */ public static Set<String> pTypes(String inputFileName) throws FileNotFoundException //Construct a tree set to return the property types { Set<String> type = new TreeSet<String>(); Scanner in = new Scanner(new File(inputFileName)); // Use delimiters to select specific chars for set in.useDelimiter("[1234567890. ]"); while (in.hasNext()) { type.add(in.next().toUpperCase()); } in.close(); return type; } /** Reads the input file. @param inputFileName @returns the Agent id's and corresponding property values. */ public static Set<String> agentValue(String inputFileName) throws FileNotFoundException { TreeSet<String> tail = new TreeSet<String>(); SortedMap<String, Number> agentValues = new TreeMap<String, Number>(); Scanner in = new Scanner(new File(inputFileName)); String line = inputFileName; while (in.hasNext()) { try { line = in.nextLine(); String[] fields = line.split("[\\s}]"); String agentId = (fields [3]); Double pValue = Double.parseDouble(fields [2]); if (agentValues.containsKey(agentId)) { pValue += agentValues.get(agentId).doubleValue(); } agentValues.put(agentId, pValue); }catch (Exception e) { } // Create keyMap with all keys and values Set<String> keySet = agentValues.keySet(); for (String key : keySet) { Number value = agentValues.get(key); //System.out.println(key + "\t" + value); tail.add(key + "\t" + value); } } return tail; } }