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 5 of 5

Thread: Help With I/O Exception Handling!

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.KETTask2pt1.zipKETTask2pt1.zip
    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;

    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; 
       }
     
           }
    Last edited by jps; August 25th, 2013 at 03:14 PM. Reason: code tags


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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:
            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.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    26
    My Mood
    Where
    Thanks
    8
    Thanked 1 Time in 1 Post

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

  4. #4
    Junior Member
    Join Date
    Aug 2013
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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!!

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help With I/O Exception Handling!

    And you're welcome regardless.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    BTaggar (August 26th, 2013)

Similar Threads

  1. Help with exception handling.
    By K0414 in forum Exceptions
    Replies: 3
    Last Post: April 19th, 2013, 02:18 PM
  2. need help in exception handling
    By gauravdudeja in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2013, 06:14 AM
  3. Exception handling
    By JavaGirl9001 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 26th, 2011, 08:45 PM
  4. Exception Handling
    By hello_world in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 5th, 2011, 05:47 PM
  5. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM