Hi guys, so my task is as follows.
A. 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.

Note: Agent id would be the key, and accumulated total of property listed would be the value.

• 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.

So I have written the code, and my output file is missing data.

Here is an example of the input and then the output files (what they should look like)
Example listings.txt file:

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

Example agentreport.txt file:

COMMERICAL
FARM
LAND
RESIDENTIAL

101 600000.00
105 30000.00
106 200000.00
107 1040000.00
110 250000.00

When I process the output file i only get

COMMERCIAL   
COMMERCIAL    
FARM               
LAND                
RESIDENTIAL      
RESIDENTIAL       

here is my code

package propertylisting;

import java.io.*;
import java.util.*;
import java.lang.Integer;

/**
 *
 * @author James
 */
public class PropertyListing {

    /**
     * @param args the command line arguments
     */
    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:\\agentreport.txt", true));
             } catch (IOException e ){
                }
                PrintWriter pwo = new PrintWriter(pwfo);

                //Now we construct the Property Type treeSet.

                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();
                pwo.close();
                }
                /**
                 * 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<>();
                    Scanner in = new Scanner(new File(inputFileName));

                    // Use the Delimiter to select specific characters for the set.
                    in.useDelimiter("[1234567890.]");

                    while (in.hasNext())
                    {
                        type.add(in.next().toUpperCase());
                                }
                    in.close();

                    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;
                }
    }

I'm not sure why it is happening... it would make sense if nothing printed... but half of it prints, with a huge blank above it.

Recommended Answers

All 4 Replies

Can you post what the agentreport.txt file should contain? Here's what I get:

>

COMMERICAL
FARM
LAND
RESIDENTIAL

101 600000.00
105 30000.00
106 200000.00
107 1040000.00
110 250000.00

Add some debug println statements in the pTypes method to show what is read from the input file and written out so you can see what the computer is seeing.

What you have there is exactly what the Agentreport.txt should contain.... Did you copy my code exactly or make a change somewhere?

I did not make any changes.

after, try for a couple of hours thinking its my code. it was actually the txt file it was reading, it was formatted incorrectly, and therefore not reading the file. Now i feel stupid... thanks for checking my code again though :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.