1 Attachment(s)
Help With I/O Exception Handling!
I am new to java programming and I need help with an assignment: I have most everything figured out, but I am stuck with a particular issue listed below. Here is what I am trying to accomplish and the issue I am having, I need to:
Develop an application that reads your listings.txt file
analyzes the property listed per agent
output 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.Attachment 2280Attachment 2280
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
Basically I the totals are adding up correctly, but for some reason I can not get rid of the first 101 (101: 500000) and the first 107 (107:1000000.0). What can I do to remove those particular lines, but keep everything else as is? Any help on this would be appreciated! The current code is below and attached as a zipped project.
Code:
package homework;
Code java:
import java.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;
import java.util.*;
import java.io.*;
public class homework {
public static void main(String[] args)
throws FileNotFoundException{
// Ask for the file name listings.txt & creates BufferedWriter
Scanner input = new Scanner(System.in);
System.out.print("Please enter listings.txt: ");
String listingsinput = input.next();
BufferedWriter Writer = null;
try{
Writer = new BufferedWriter(new FileWriter("agentreport.txt", true));
}
catch (Exception e) {
System.out.println("File can not be found");
}
try (PrintWriter Writer2 = new PrintWriter(Writer)) {
Set<String> propertyTypes = types(listingsinput);
for (String type : propertyTypes) {
System.out.println(type);
Writer2.println(type);
}
Set<String> aReport = aValue(listingsinput);
for (String info : aReport) {
{
System.out.print(info);
Writer2.println(info);
}
}
Writer2.flush();
Writer2.close();
}
}
//Creates Treeset(to alphabatize and remove duplicates) using delmiter to remove all numbers
public static Set<String> types(String listingsinput)
throws FileNotFoundException{
Set<String> type = new TreeSet<>();
try (Scanner in = new Scanner(new File(listingsinput))) {
in.useDelimiter("[^a-zA-Z]");
while (in.hasNext()) {
boolean add = type.add(in.next().toUpperCase());
}
}
return type;
}
//adds like numbers and displays underneath prior treeset
public static Set<String> aValue(String listingsinput)
throws FileNotFoundException {
System.out.println("");
TreeSet<String> info = new TreeSet<>();
SortedMap<String, Number> aTotals = new TreeMap<>();
Scanner in = new Scanner(new File(listingsinput));
String line = listingsinput;
while (in.hasNextLine()) {
try{
line = in.nextLine();
String[] fields = line.split("[\\s}]");
String aId = (fields[3]);
Double tValue = Double.parseDouble(fields[2]);
if(aTotals.containsKey(aId)){
tValue += aTotals.get(aId).doubleValue();
}
aTotals.put(aId, tValue);
} catch (Exception e) {
}
Set<String> keySet = aTotals.keySet();
for(String key : keySet) {
Number value = aTotals.get(key);
info.add(key + ":" + value);
}
}
return info;
}
}
Re: Help With I/O Exception Handling!
Please post your code in code tags.
The reason your results contain subtotals is because the following loop in the aValue() method occurs too early:
Code java:
Set<String> keySet = aTotals.keySet();
for(String key : keySet)
{
Number value = aTotals.get(key);
info.add(key + ":" + value);
}
Where you have it now, the output string is being built incrementally, including the incomplete results. If you move the loop to just before the return statement in the aValue() method, the results will be improved.
Re: Help With I/O Exception Handling!
use the code tags
[code] ...place code here... [\code]
that way it's easier for people to read your code and help direct you towards a solution.
Re: Help With I/O Exception Handling!
Wow... That was an easy fix, thanks for the help! Sorry about not placing the code in tags, I will make sure to do that in the future, but appreciate the help regardless!!
Re: Help With I/O Exception Handling!
And you're welcome regardless.