I/O Program output error

I/O Program output error

Hello All,

I am working on a program that requries me to read and analyze a .txt file and output the results to a .txt file, but I am getting incorrect output. I have been successfull with part of the program in that it reads the text file and analyzes it, however I need it to take the key(which is Agent ID) and add together the total of the value(which is pValue), however the output just shows each Agent ID and then each pValue. I do not know how to take all the AgentID's and add their pValues together to output the total. I also need the pValues to be in decimal format like(#.##), but I can't seem to get that to work either. Can anyone offer some suggestions?

I thank you in advance for any help.

Here is the code I have come up with so far:

print("code sample");

package gtt1_task2;

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 zubeda.a.hemani */

//This program reads a text file, analyzes property listings and outputs a report to a text file

public class GTT1_Task2 { /** * @param args the command line arguments */ public static void main(String[] args)throws FileNotFoundException {

    // Prompts the user for the input file name (listings.txt) by creting a scanner object

    Scanner listings = new Scanner(System.in);
    //Ask user for file name

    System.out.print("Input File:   ");
    String inputFileName = listings.next();

    //Use unbuffered file to open text file(listings.txt) and read in property listings.

    BufferedWriter pwfo = null;

    try{
        pwfo = new BufferedWriter(new FileWriter("C:\\Users\\zubeda.a.hemani\\Documents\\NetBeansProjects\\GTT1_Task2\\src\\gtt1_task2\\listings.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);
       }
       //pwo.flush();
       //pwo.close();

      //Construct agent id and value treeSet
           Set<String> agentRpt = agentValue(inputFileName);
      //Print agent id and values from key set    
           for(String tail: agentRpt)
           {   
               {
                   System.out.println(tail);
                   pwo.println(tail);
               }
           }
           pwo.flush();
           pwo.close();
           }   
 /**
    Reads the input file.
    @param inputFileNamegre
    @return the alphabetized property types in uppercase.
 */
 public static Set<String> pTypes(String inputFileName)
    throws FileNotFoundException

    //Construct a tree set to return the property types
  {
    Set<String> type = new TreeSet<String>();
    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.
    @param inputFileName
    @returns the Agent id's and corresponding property values.
 */
 public static Set<String> agentValue(String inputFileName)
    throws FileNotFoundException
 {
     TreeSet<String> tail = new TreeSet<String>();
     SortedMap<String, Number> agentValues = new TreeMap<String, Number>();
     Scanner in = new Scanner(new File(inputFileName));
     String line = inputFileName;

   while (in.hasNext())
   {
           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) {
                              }
 // 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 + "\t" + value);
                tail.add(key + "\t" + value);
                }
           }
   return tail;
   }
}
View Answers

Ads









Related Tutorials/Questions & Answers:
I/O Program output error
I/O Program output error  Hello All, I am working on a program... file, but I am getting incorrect output. I have been successfull with part of the program in that it reads the text file and analyzes it, however I need it to take
Write a program that replaces a, e, i, o, u in Java2
Write a program that replaces a, e, i, o, u in Java2  Write a program that replaces a, e, i, o, u, with the letter z. (i.e. John Smith -> Jzhn Smzth. Also the program reverses the users name (i.e. John Smith -> htimS nhoJ
Advertisements
i/o
i/o  java program using inputstream and outputstream   Hi Friend, Try the following code: import java.io.*; class InputStreamAndOutputStream { public static void main(String[] args)throws
i/o
i/o  java program using inputstream and outputstream   Hi Friend, Try the following code: import java.io.*; class InputStreamAndOutputStream { public static void main(String[] args)throws Exception
i/o
i/o  java program using inputstream and outputstream   Hi Friend, Try the following code: import java.io.*; class InputStreamAndOutputStream { public static void main(String[] args)throws Exception
i/o
i/o  java program using inputstream and outputstream   Hi Friend, Try the following code: import java.io.*; class InputStreamAndOutputStream { public static void main(String[] args)throws Exception
output error
output error  this is my program import java.io.*; public class separate { public static void main(String[] args)throws IOException...]; for (k=0;k } } if i enter "my name" as the input the output
I/O stream class.
I/O stream class.  Explain the hierarchy of Java I/O stream class.   Hierarchy of Java I/O streams Have a look at the following link: Java I/O
Java I/O problem
Java I/O problem   Write a Java application that prompts the user... by line, and output it to the console with the line number. The program should.... The program should use the FileWriter class and an appropriate processing stream
I/O to another applications
I/O to another applications  **What if there exists an application that needs a certain input and will give an output. Using Java, how can we invoke that application with an input and read the resulted output?**   Hi
i/o streamas
i/o streamas  java program using bufferedreader and bufferedwriter   Hi Friend, Try the following code: import java.io.*; class BufferedReaderAndBufferedWriter{ public static void main(String[] args) throws
I/O Java
I/O Java  import java.io.File; import java.io.FileNotFoundException... System.out.println(" Error in Concat:"+e); } } }   I am not really sure why... (FileNotFoundException e) { System.out.println(" Error: "+e + " "+ inputFile
output of the program
output of the program  public static int sum(List list) { int sum = 0; for ( Iterator iter = list.iterator(); iter.hasNext(); ) { int i = ((Integer)iter.next()).intValue(); sum += i; } return sum
java i/o operations
java i/o operations  how to write integer data to a file in java using random access file object or file writer or data outputstream i have already tried the write and writeInt methods....plz help
Java i/o opearations
Java i/o opearations  "FOLLOWING IS MY LAST QUESTION ON JAVA I/O... to a file in java using random access file object or file writer or data outputstream i..."),true)); for(int i=1;i<=10;i++){ System.out.print("Enter Number
find a substring by using I/O package
find a substring by using I/O package  Write a java program to find a sub string from given string by using I/O package
File I/O
File I/O  i am trying to read and write a file. my program works perfectly i am using PrintWriter and BufferedReader. but my problem is that when... a computer i am running linux i am of ubuntu output i want is: line1. i am
File I/O
File I/O  i am trying to write a program that reads a text file and writes it to another directory. the main purpose of this program is to take the text file then read it and write it into as a comma delimitade file. i have
File I/O
File I/O  greetings i was running into a problem. is their a way...(); File[] files = file.listFiles(filter); for (int i = 0; i < files.length; i++) { System.out.println("Processing " + files[i].getPath
File I/O
File I/O  i have a problem i am trying to print on a new line every time i reach a certain condition "if(line.startsWith("Creating"))" i want... this is my code package elite.tech.com; /*********** * program Cvs
Java I/O Byte Streams
Java I/O Byte Streams In this section we will discussed the I/O Byte Streams. This Stream handles the 8-bit binary input/output of data. Byte streams... I/O raw binary data the byte stream classes are defined. For all of the byte
Java I/O Character Streams
is used for output (write to). To work with the file I/O specialized classes...Java I/O Character Streams In this section we will discuss the I/O Character... and for this Java provides the Character stream I/O. Character stream I/O
Java I/O From the Command Line
Java I/O From the Command Line In this section we will learn about the I/O.... In this way an input is taken using the keyboard and output is written to the display... System.in Standard Output : Standard Output can be gotten using System.out
Java I/O Buffered Streams
Java I/O Buffered Streams In this section we will discuss the I/O Buffered Streams. In Java programming when we are doing an input and output operation then we are trying to interact our Java program to read from or write data
Java I/O Data Streams
Java I/O Data Streams In this tutorial we will discuss the Java I/O Data Streams. To deal with the binary I/O of primitive data type values as well... the Java primitive data type values to the output stream. 
Creating a File I/O using import scanner, io.* and text.*
Creating a File I/O using import scanner, io.* and text.*  open a file for reading and open another file for writing data. input file hours-int, hourly rate-double, name-string. output file name-string, hours-int, hourly rate
Shortest the program as possible but output will not change
Shortest the program as possible but output will not change  #include...; } } int product(int m, int n){ int sum; int i; sum = 0; for(i = 0; i<n; i++){ sum = sum + m; } return sum; } void jw_insertion_sort ( void
Shortest the program as possible but output will not change
Shortest the program as possible but output will not change  #include...; } } int product(int m, int n){ int sum; int i; sum = 0; for(i = 0; i<n; i++){ sum = sum + m; } return sum; } void jw_insertion_sort ( void
why the program is showing error?
why the program is showing error?  <%@page import="java.sql.*"%>...=Integer.parseInt(rs1.getString(10)); 36: i=i+k; An error occurred at line..."); Integer i=0; out.println("hi"); while(rs1.next
ERRor in executing DB program
ERRor in executing DB program  While executing following code an Error was generated.can any one help me for this. LiveDB liveDBObj... = liveDBObj.getPreparedStatement(qry); pstmt.setDouble(1,2.0); pstmt.setString(2,"name1"); int i
How to get the output of jsp program using Bean
the ouput.i want the output of the program .i posted the same program below so... already did that one. Now i want the output of the program ,so please tell me...How to get the output of jsp program using Bean  Hello my Roseindia

Ads