Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: I/O AND EXCEPTION HANDLING need help

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I/O AND EXCEPTION HANDLING need help

    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]


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I/O AND EXCEPTION HANDLING need help

    Can you post the program's current output and add some comments to it saying what is wrong with it.

    it does not drop the first value
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I/O AND EXCEPTION HANDLING need help

    The programs current out put is:

    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

    the original file it is pulling the information from is the listings.txt which looks like:
    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

    for example the agent ID for the first two entries is 101 so it is suppose to add 5000000.00 and 1000000.00 and get 6000000.00 and drop the 5000000.00 but it adds them together but does not drop the 5000000.00 so the out put is 6000000.00 and it is keeping the 5000000.00. So after I run the program the end result should be:

    COMMERICAL
    FARM
    LAND
    RESIDENTIAL

    101 600000.00
    105 30000.00
    106 200000.00
    107 1040000.00
    110 250000.00

    to the agentreport.txt report I hope this clarifies things

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I/O AND EXCEPTION HANDLING need help

    Is this the problem with the output:
    Current:
    101:500000.0
    101:600000.0
    Desired:
    101 600000.00

    One difference I see is there is a : instead of a space between the numbers on a row
    Another difference is that there are two rows that start with 101 vs one row

    Why does the code insert the : between the numbers if you want a space there?


    How does the code chose what to write out? Where do the values come from?
    Where should the code remove the extra values that you don't want written out?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I/O AND EXCEPTION HANDLING need help

    Yes that is correct. The 101 is the agent ID and the 5000000.00 is the listing price for the commercial property and the 100000.00 is the listing price for the residential property and it is suppose to add both property prices together for that agent so ya for agent 101 the out come should be "101 600000.00"

    I did get the : out not it is a space so that's good.

    Where the values come from are the listings.txt file I was given that as the information I needed to use
    listing.PNG

    this is exactly how I was given the text file to use. so if there are multiple properties per agent I just need the program to add the total dollar amount per agent number and display the total

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I/O AND EXCEPTION HANDLING need help

    Try debugging the program by printing out the contents of the Map and the Set so you can see what data is available in each.

    I just need the program to add the total dollar amount per agent number
    Who wrote the code that you posted? Have you read the code to see what it does?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I/O AND EXCEPTION HANDLING need help

    I think I got it figured out thanks for the help

Similar Threads

  1. [SOLVED] exception handling
    By Topflyt in forum Exceptions
    Replies: 5
    Last Post: December 22nd, 2011, 12:00 PM
  2. [SOLVED] Exception Handling
    By Melawe in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2011, 07:39 PM
  3. Exception Handling
    By hello_world in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 5th, 2011, 05:47 PM
  4. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM