I am new to java programming and I in my first programming class I need help with how to fix my code what I need to do is:
Develop an application that reads your listings.txt file, analyzes the property
listed per agent, and outputs a report to an agentreport.txt file. Your
application should do the following:
1. Prompt the user for the name of the input file (listings.txt).
2. Open listings.txt file and read in property listings.
3. Store each property type into a Set.
a. Convert property type to upper case before adding to your Set using
method(s) from String class.
b. Sort your Set of property types alphabetically.
4. Use a Map to calculate total property listed in dollars and cents for each
agent id.
• Sort your Map by agent id.
• Create an agentreport.txt file.
5. Use an Iterator to iterate through your Set and write your sorted set of
property types sold by the agents to the agentreport.txt file.
6. Iterate through your Map to write your sorted pair of agent id and total
property listed to the agentreport.txt file.
Example to the listings.txt file looks like this:
110001 commercial 500000.00 101
110223 residential 100000.00 101
110020 commercial 1000000.00 107
110333 land 30000.00 105
110442 farm 200000.00 106
110421 land 40000.00 107
112352 residential 250000.00 110
What the agentreport is suppose to look like is this:
Example agentreport.txt file:
COMMERICAL
FARM
LAND
RESIDENTIAL
101 600000.00
105 30000.00
106 200000.00
107 1040000.00
110 250000.00
what my agentreport.txt output looks like is this:
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
for some reason commercial, farm, land, and residential are moves slightly to the right and the amounts add up but it does not drop the first value I have been banging my head against the wall for hours
over this and can not figure out why it will not drop the value and just keep the total. any help would be greatly appreciated.
package property_listings_report; 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 AlanDesktop */ public class PropertyListingsReport { public static void main(String[] args) throws FileNotFoundException { //prompts the user to input a file name Scanner console = new Scanner(System.in); System.out.print("Input file: "); String inputFileName = console.next(); BufferedWriter pwfo = null; try { pwfo = new BufferedWriter(new FileWriter("C:\\Users\\AlanDesktop\\Documents\\NetBeansProjects\\Task 2\\KET Task 2_A\\agentreport.txt", true)); } catch (IOException e) { } try (PrintWriter pwo = new PrintWriter(pwfo)) { Set<String> propertyTypes = pTypes(inputFileName); // Print the property types from treeSet for (String type : propertyTypes) { System.out.println(type); pwo.println(type); } // Now to contruct agent ids and values treeSet Set<String> agentRpt = agentValue(inputFileName); // Then we can print the agents id's and for (String tail : agentRpt) { { System.out.println(tail); pwo.println(tail); } } pwo.flush(); } } /** * Reads the Input file * * returns the alphabetized property types in correct format */ public static Set<String> pTypes(String inputFileName) throws FileNotFoundException // Now contruct tree set to return the property types { Set<String> type = new TreeSet<>(); try (Scanner in = new Scanner(new File(inputFileName))) { in.useDelimiter("[1234567890.]"); while (in.hasNext()) { boolean add = type.add(in.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.hasNextLine()) { 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) { } // Code to create a keyMap with all keys and values. Set<String> keySet = agentValues.keySet(); for (String key : keySet) { Number value = agentValues.get(key); tail.add(key + ":" + value); } } return tail; } }[ATTACH]1738[/ATTACH]