Results 1 to 6 of 6
  1. #1
    LyellN is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Not Reading Entire File

    I'm new to Java and have not been able to figure this out.
    I'm reading a text file that contains 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

    The output goes to a text file and should look like this:
    COMMERICAL
    FARM
    LAND
    RESIDENTIAL

    101 600000.00
    105 30000.00
    106 200000.00
    107 1040000.00
    110 250000.00

    I'm creating the output file, but it only contains the categories and not the details below them. Cant seem to figure out why. Any help would be appreciated.
    Java Code:
          package propertylistings;
       
           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;
       
          public class propertylistings {
      
                     public static void main(String[] args)
      
                      throws FileNotFoundException
      
              {
      
              // Prompt for the input 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("agentReport.txt", true));
                  } catch (IOException e) {
                    }
                  PrintWriter pwo = new PrintWriter(pwfo);
       
              //Construct property type treeSet
      
                  Set<String> propertyTypes = pTypes(inputFileName);
      
               // Print property types from treeSet
      
                for (String type : propertyTypes)
                   {
                   System.out.println(type);
                    pwo.println(type);
               }
                
               //Construct agent ids and values treeSet
      
                   Set<String> agentRpt = agentValue(inputFileName);
       
              // Print agent Ids and values from key set
      
                  for (String tail: agentRpt)
                  {
                     {
                     System.out.println(tail);
                     pwo.println(tail);
                     }
                  }
                   pwo.flush();
                   pwo.close();
                  }
             
               //   Reads the input file.
               //   @return the alphabetized property types in uppercase.
               
               public static Set<String> pTypes(String inputFileName)
                  throws FileNotFoundException
      
                //Construct a tree set to return property types
                {
                  Set<String> type = new TreeSet<>();
                  Scanner in = new Scanner(new File(inputFileName));
      
                  // Use delimiters to select specific chars for set
      
                  in.useDelimiter("[1234567890. ]");
                  
                  while (in.hasNext())
                  {
                    type.add(in.next().toUpperCase());
                  }
                  in.close();
                  return type;
                }
              //  Reads the input file.
              //  @returns the Agent id's and corresponding 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;
                   //String[] fields;
       
                 while (in.hasNextLine())
                {
                    try {
                  line = in.nextLine();
                  String[] fields = line.split("\\s");
                  //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) {
                     }
     
              // Create keyMap with all keys and values
     
                     Set<String> keySet = agentValues.keySet();
                     for (String key : keySet)
                         {
                     Number value = agentValues.get(key);
     
                         //System.out.println(key + ":" + value);
     
                         tail.add(key + ":" + value);
                         }
                      }
                
                return tail;
                }
         }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Not Reading Entire File

    Can you post the program's output so we can see what it does?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    LyellN is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Not Reading Entire File

    Here is the output.
    COMMERCIAL
    FARM
    LAND
    RESIDENTIAL

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Not Reading Entire File

    Try debugging the code by adding some println statements to show where the code is executing and what the values of variables are as the code executes.
    For example print out the value of line every time a new line is read.
    Print out a message where the code decides what to write to the file showing the values of the variables that control if the code writes to the file.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    LyellN is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Not Reading Entire File

    I went through and printed out the variables and here are the results.
    variable line - inserted a print statement after line 102
    Result: (Prints the contents of the file)
    COMMERCIAL
    FARM
    LAND
    RESIDENTIAL
    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

    Printing the variable fields - inserted print statement after line 105
    Results: (Not sure what this tells me)
    COMMERCIAL
    FARM
    LAND
    RESIDENTIAL
    [Ljava.lang.String;@5054c2b8
    [Ljava.lang.String;@543492fc
    [Ljava.lang.String;@479e0994
    [Ljava.lang.String;@5886f847
    [Ljava.lang.String;@5162ab9
    [Ljava.lang.String;@724eda4a
    [Ljava.lang.String;@16dd6ae7

    After that no other variables print out.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Not Reading Entire File

    The printed lines should have labels so you can tell what was printed. The output that starts with [Ljava are for String arrays. Use the Arrays class's toString() method to format an array for printing
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 12-15-2012, 01:37 AM
  2. passing an entire file to a c++ function
    By P5music in forum New To Java
    Replies: 3
    Last Post: 10-01-2010, 04:49 PM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. Replies: 15
    Last Post: 03-18-2009, 10:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •