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

Thread: counting the values in input file and and writing the output to a file

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default counting the values in input file and and writing the output to a file

    Can any one please correct this code. This is to read a file and later it should write the output to a file. Input file has 20 rows and 4 columns. If we have 1 in the 1st column of the input file then it should count all 1's and write those 1s counted value to a file.
    Code:

    import java.io.*;
    import java.util.*;
    import java.lang.ClassNotFoundException;
    class Code
    {
    public static void main(String[] args) throws IOException
    {
    String str[][] = new String[20][4];
    FileReader fr = new FileReader("in.txt");
    BufferedReader br = new BufferedReader( fr );
    FileWriter fw = new FileWriter("out.txt");
    PrintWriter pw = new PrintWriter(fw);
    String line = "";
    int i = 0, j= 0, one=0, two=0,three=0;
    while( ( line = br.readLine() ) != null )
    {
    StringTokenizer tok = new StringTokenizer(line, ", ");
    if(str[i][0].equals("1") )
    {
    one = one++;
    }
    else if(str[i][0].equals("2") )
    {
    two = two++;
    }
    else if(str[i][0].equals("3") )
    {
    three = three++;
    }
    i++;
    }
    br.close();
    fr.close();
    System.out.println("Sending the output to file:");
    for(i=0;i < 20; i++)
    {
    for(j=0;j<4; j++)
    {
    System.out.printf("one" +one);
    pw.print(one);
    System.out.println("two" +two);
    pw.print(two);
    System.out.println("three" +three);
    pw.print(three);
    }
    pw.println();
    }
    pw.close();
    fw.close();
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: counting the values in input file and and writing the output to a file

    Can any one please correct this code
    We can help YOU correct it, provided you tell us what's wrong in the first place. And please use the code tags, and please be forthright when crossposting

    This thread has been cross posted here:

    http://www.roseindia.net/answers/viewqa/Java-Beginners/26756-counting-the-values-in-input-file-and-and-writing-the-output-to-a-file.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Last edited by copeg; July 4th, 2012 at 09:15 AM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    srujirao (July 4th, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: counting the values in input file and and writing the output to a file

    As i am new to these forums i really don't know about this. I have gone through the links which you have mentioned about the cross posting. Thanks for telling this.

    I didn't get what you are asking about. " provided you tell us what's wrong in the first place. "

  5. #4
    Junior Member ShadeDarkan's Avatar
    Join Date
    Jul 2012
    Location
    Houston, TX
    Posts
    13
    Thanks
    0
    Thanked 4 Times in 3 Posts

    Default Re: counting the values in input file and and writing the output to a file

    You are accessing your String Array from the While loop, but you haven't populated it with anything. That is going to throw and error. The next problem is that you are breaking up the input line with StringTokenizer, but you don't do anything with the data. Your while loop isn't actually comparing and counting any of the input data so your output file is just going to be a bunch of zeros.
    We need some more clarification on what exactly the program should be doing. I don't understand exactly what should be output into a file and what you are trying to count from the input.

Similar Threads

  1. Writing to output text file
    By gatorsgirl in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2012, 08:17 PM
  2. Writing output from a void method to a file
    By TheWhopper858 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 05:21 AM
  3. Input output file help
    By peteyfresh12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 07:44 AM
  4. Input/Output file help
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 06:26 PM
  5. Writing Output To New File
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 6th, 2010, 07:25 PM

Tags for this Thread