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

Ads
 

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(); } }

View Answers

July 4, 2012 at 8:50 AM

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();
    }
}

July 4, 2012 at 9:50 AM

please correct this code...

Ads









Related Tutorials/Questions & Answers:
counting the values in input file and and writing the output to a file
counting the values in input file and and writing the output to a file ...-code/16483-counting-values-input-file-writing-output-file.html this code... it should write the output to a file. Input file has 20 rows and 4 columns. If we
How to create file from input values in Jframe ?
How to create file from input values in Jframe ?  hi i m doing my project using java desktop application in netbeans.i designed a form to get... to take these details and make a file that can be appended each time.how
Advertisements
File saving and Writing
File saving and Writing   Hello Rose india..... I have a doubt... values... What i want to ask is "I want to create a jsp page which will include a "file create option" it will be ".csv" file and i want to write the data from
Counting specific word occurences in a file
Counting specific word occurences in a file  Hello I have a log file from the proxy which consists all the browsing history with date,time,url,ip address etc as fields.Now i need to take only main part of the url like the domain
writing and appending to a file
writing and appending to a file  My input file includes data from... and values and writes into an existing file for 'male'. How can I do the same... am confusing you!!! But I want something like "going through my input
Working With File,Java Input,Java Input Output,Java Inputstream,Java io Tutorial,Java io package,Java io example
it, deleting it, reading from or writing to it. The constructors of the File class... Working With File      ... provide a simple model for reading and writing data. However, streams don't support
Take input in batch file
Take input in batch file  How to take input in batch file? Thanks   Hi, You can use the following line of code in your batch file: set /p myfile=Enter file name: In the above code myfile variable will hold the data
Writing and Reading A File
Writing and Reading A File  Hello, I've been trying to learn writing and reading data from file for our assignment, but just stuck on how to proceed... (FirstName, LastName, Age). I also made an empty file I named "contactsFile.txt" where
input output
It uses for writing data to a file and also implements an output... Input And Output       Introduction The Java I/O means Java Input/Output and is a part
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
_File = input.nextLine(); if(Input_File.equalsIgnoreCase(Output_File...Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write
ModuleNotFoundError: No module named 'file_output'
ModuleNotFoundError: No module named 'file_output'  Hi, My Python... 'file_output' How to remove the ModuleNotFoundError: No module named 'file_output' error? Thanks   Hi, In your python environment
Java Input/Output Examples
Java Input/Output Examples In this tutorial you will learn about how the Inputs and outputs are managed in java. To understand the Java Input & Output... and interfaces to handle the system input & output through data streams
File Handling Classes in Java
In this section, you will get to know about file handling classes in java to handle the file input output operations
How to write a file in Java?
data into output stream. It is used for writing character files. By default... that allows data writing in output stream in various ways. Some...How to write a file in Java? To write a file in Java use the class FileWriter
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
of the output file - "); String Output_File = input.nextLine(); if(Input...Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write
Redirecting the console output into a file in Java
Redirecting the console output into a file in Java  Hi, i coded a program that read specific lines from a text file. It's working well. Now i want to redirect the output of the console to a text file. i used your tutorial
Writing Log Records to a Log File
Writing Log Records to a Log File       This section demonstrates for writing log records to a log file. Logger provides different types of level like: warning, info
ModuleNotFoundError: No module named 'wishbone_output_file'
ModuleNotFoundError: No module named 'wishbone_output_file'  Hi...: No module named 'wishbone_output_file' How to remove the ModuleNotFoundError: No module named 'wishbone_output_file' error? Thanks   Hi
ModuleNotFoundError: No module named 'wishbone_output_file'
ModuleNotFoundError: No module named 'wishbone_output_file'  Hi...: No module named 'wishbone_output_file' How to remove the ModuleNotFoundError: No module named 'wishbone_output_file' error? Thanks   Hi
Writing to and reading from a binary file in java.
Writing to and reading from a binary file in java.  I have written the following code to convert an ASCII text file to a binary file: public static void main(String args[]){ DataOutputStream dos = null; Scanner input = null
ModuleNotFoundError: No module named 'openQCD-input-file-editor'
ModuleNotFoundError: No module named 'openQCD-input-file-editor'  Hi...: No module named 'openQCD-input-file-editor' How to remove the ModuleNotFoundError: No module named 'openQCD-input-file-editor' error? Thanks  
How to read the contents in a file which is of input type file
How to read the contents in a file which is of input type file  I have a jsp page wherein i have to passs the file as input and read the contentsn...-data" ACTION="upload"> File to upload: <INPUT TYPE=FILE NAME="upfile"><
reading dropdown values from properties file in jsp
reading dropdown values from properties file in jsp  reading dropdown values from properties file in jsp
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks..... after line number four my text will display in text file using java program  ... = ""; int lineNo; try { File f=new File("c
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks..... after line number four my text will display in text file using java program  ... = ""; int lineNo; try { File f=new File("c
input output
input output  java program using fileinputstream and fileoutputstream   Hi Friend, Try the following code: import java.io.*; class...(); System.out.println("File is copied"); } } Thanks
input output
input output  java program using fileinputstream and fileoutputstream   Hi Friend, Try the following code: import java.io.*; class...(); System.out.println("File is copied"); } } Thanks
Count repetitions of every word from an input file
Count repetitions of every word from an input file  Hello..i got to know how can i count the repetitions of every word present in a specific input file?For suppose if i have a url log file which have time,url's,ip address etc
insert values from excel file into database
insert values from excel file into database   hi i want to insert values from Excel file into database.Whatever field and contents are there in excel... the following link: Insert values from excel file to database
problem of writing to a local file ( JApplet ) - Applet
problem of writing to a local file ( JApplet )  Dear All, I want to program a guestbook using java applets but now I have problem of writing to a file from JApplet. it is working without any problem if i run the program using

Ads