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

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









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
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
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
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
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
Working With File,Java Input,Java Input Output,Java Inputstream,Java io Tutorial,Java io package,Java io example
Working With File      ... provide a simple model for reading and writing data. However, streams don't support all the operations that are common with a disk file. In lesson, we will learn how
File Writing - Java Beginners
File Writing  Hi... I need a syntax or logic or program by which we can write into the desired shell of the Excel file from console window...  Hi friend, I am sending you a link. This is will help you. Please
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
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
Writing xml file - Java Beginners
Writing xml file  Thank you for the quick response The values which... values from the database. If it is still confusing i'l post you my question again... XmlServlet().createXmlTree(doc); System.out.println("Xml File Created
Java Redirect output to file
Java Redirect output to file This section illustrates you how to redirect the output to the file. It may be comfortable to a programmer if they are able... to the file. In the given example, we have set the output stream of System
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 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
How to write a file in Java?
data into output stream.ADS_TO_REPLACE_1 It is used for writing character files... of FileWriter class that allows data writing in output stream in various ways.ADS...How to write a file in Java? To write a file in Java use the class FileWriter
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
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
Writing a file using servlets - JSP-Servlet
Writing a file using servlets  I'm using a servlet to read an input... reading the data and creating a file in my space. but it is not writing..., but its not writing in the xyz.txt file. I think writing in a servlet should
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
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
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
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  
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
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"><
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
java program for writing xml file - Java Beginners
java program for writing xml file  Good morning i want to write values from my database(one table)into one xml file. Like i have 3 coloumns in my table along with their values,so when click some button i need to generate one
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
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
reading dropdown values from properties file in jsp
reading dropdown values from properties file in jsp  reading dropdown values from properties file in jsp
how to display the output of the newly created excel file
how to display the output of the newly created excel file  the following program will create the test.xlsx file, how can i display the content of the test.xlsx file in the web browser? thanks, %@ page import
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
jar file not reading input from serial port
jar file not reading input from serial port  i used a coding... when i execute thru jar file. but when i execute thru netbeans, data is received... { System.out.println("Getting input Stream"); inputStream
jar file not reading input from serial port
jar file not reading input from serial port  i used a coding... when i execute thru jar file. but when i execute thru netbeans, data is received... { System.out.println("Getting input Stream"); inputStream
Input and Output package
Input and Output package  Hi guys, My professor used input and output... she used in.readint() out.writeln() commands to read input and print output. she created two new objects directly to use this statements. /* input and output
how to fetch values from .properties to a html file
how to fetch values from .properties to a html file  I have a .properties file with some key value pairs in it. I need to fetch the values from this .properties file into a html file.Please let me know how to do
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
Show output as a xml file using Velocity
Show output as a xml file using Velocity       This Example shows you how to show output as a xml file.... With the help this list object we will create output for all the given values
Writing to File in Java
Writing to File in Java Writing any character or text in Java file is possible... file) This is the simplest example of the class used for writing character file... are used for writing Character files and Text Files respectively while another class
Data input & output Stream
Data input & output Stream  Explain Data Input Stream and Data Output Stream Class.   DataInputStream and DataOutputStream A data input stream lets an application read primitive Java data types from an underlying input
input output in java
input output in java  java program using filereader and filewriter...(); System.out.println("File is copied"); } } Thanks   Hi...); } out.close(); System.out.println("File is copied"); } } Thanks
without writing web.xml file we can write servlet program
without writing web.xml file we can write servlet program  Sir Morning... Sir I have one Question "without writing web.xml file we can write servlet program". if yes which way? if no why? but without use Annotation........ Plz
Passing values in ComboBox from XML file
Passing values in ComboBox from XML file In this tutorial we are going to know how we can pass a values in ComboBox by using XML.  This example... from an XML document. For this what we need  a XML file in which we have
File
File  How to create .Exe file in jana
file
file  how can copy the information in the file to the string?   Store file data into String
file
file  Could anyone please help me to write a code that does the following: Opens a file named MyName.txt, reads the first line from the file and displays it, and then closes the file. Thank you so much

Ads