link to old post shows code: http://www.daniweb.com/software-development/java/threads/440001/output-incomplete-text-file
program to:
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 run the program I only get
COMMERICAL
FARM
LAND
RESIDENTIAL
to show up in agent agentreport.txt.

What should the listing.txt file actually look like? does it need any code to format it correctly?

thanks

it doesn't need extra formatting. read every line as a String, and use the split method to extract an array of Strings for each line. as long as you know what each element should (be able to) do, and is ..

the txt file only contains this info:
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

if my split code looks like this:

  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){
            }

how does the split know which element is agentId vs pValue. There are 4 fields but I only have a split for field 3 and 2. do I need to change the txt doc somehow?

thankyou

String[] fields = line.split("[\s}");

Do you know what regular expression is? If you do, then I would ask you to go back and read about regular expression format. If you don't, I am telling you that your regular expression is wrong. You must need to know what '[' and '}' mean. And when you put something in between a pair of '[]', it could give you a different meaning. In other words, you do not need to use the pair in your regular expression.

ah, I see I missed a ] = String[] fields = line.split("[\s}"); needs to be String[] fields = line.split("[\s}]");

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,

I thought my issue was the + symbol here: ropValue += agentValues.get(agentnumber).doubleValue();

removed it and of course it only shows me:
COMMERCIAL
FARM
LAND
RESIDENTIAL
101: $500000.0
105: $30000.0
106: $200000.0
107: $1000000.0
110: $250000.0
so it is not adding now, how do I get it to add and only show 1 row for each agent?

my working code:


/*
 * 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 Q
 */
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;

    }

    }

Also there is a part B for this task where you print to an overview.txt file. Here is my working code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package overviewapp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Q
 */
public class OverviewApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    throws FileNotFoundException, IOException
            //Prompt the user for the name of the input file (listings.txt).
    {
        Scanner getfile = new Scanner(System.in);
        System.out.print("Input file name: "); //this displays for the user
        String inputFileName = getfile.next();//this will retrieve the file listings.txt

//Open the listings.txt file and read in property listing information using a buffered FileReader.
        String line= null ;
        PrintWriter display = new PrintWriter(new FileWriter("overview.txt"),true);
        File input = new File(inputFileName);
        BufferedReader addinfo = new BufferedReader(new FileReader(input));

        //create variables 
        int count = 0;
        double sum =0;
        //Count the total number of property listings for sale
        //Calculate the total value of property for sale.
        while((line = addinfo.readLine())!=null)
        {
            count++;
                    String[]properties = line.split("[\\s}]");
                    Double propertyvalue = Double.parseDouble(properties[2]);
                    {sum+=propertyvalue;

                    }
        }
   //Use buffered FileWriter to write the count of the number of property listings to your overview.txt file.
//Use a buffered FileWriter to write the total value of properties currently for sale.          
        display.println("Total properties listed: " + count);
//Use a buffered FileWriter to write the total value of properties currently for sale.          
        display.println("Total value of properties listed: "+ sum);
        addinfo.close();

        //Store each property id into an ArrayList.
        try
        {
            ArrayList<String> propertyId = new ArrayList<>();
        Scanner idlist = new Scanner((input));

// loop to iterate through the sorted ArrayList and write property ids to overview.txt file
        while (idlist.hasNextLine())
                {
                    line = idlist.nextLine();
                    String[]fields = line.split("[\\s}]");
                    String propertyNumber = (fields[0]);

                    {
  //Use buffered FileWriter to write the total value and the total number of properties for sale.
                        propertyId.add(propertyNumber);
                        display.println(propertyNumber);
                        addinfo.close();

                    }
                }
                //this closes the print writer
                display.flush();
                display.close();
        }
        catch
                (Exception e){System.out.println("error:" + e);
        }


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