Home Answers Viewqa Java-Beginners counting the values in input file and and writing the output to a file

 
 


sam
counting the values in input file and and writing the output to a file
2 Answer(s)      10 months ago
Posted in : Java Beginners

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 Pages:
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
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
Input And Output
to another file. This topic is related to the I/O (input/output) of java.io package... Input And Output       Introduction The Java I/O means Java Input/Output and is a part of java.io
Counting bytes on Sockets,java tutorial,java tutorials
to count the bytes is to have two decorators for the input and output streams...Counting bytes on Sockets 2002-10-09 The Java Specialists' Newsletter [Issue 058] - Counting bytes on Sockets Author: Dr. Heinz M. Kabutz If you
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
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
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
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
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
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
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
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
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
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 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 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
Counting
Counting  Pl sir make a program which generate the following output on pop up menu. 1 12 123 1234 12345 123456 1234567 12345678 123456789   Java Print Pattern 1 12 123 1234 12345 123456 1234567 12345678 123456789
Counting Objects Clandestinely - Java Tutorials
Counting Objects Clandestinely 2001-12-28 The Java Specialists' Newsletter [Issue 038a] - Counting Objects Clandestinely Author: Dr. Heinz M. Kabutz... Java programmers ;-). Counting Objects Clandestinely A few months ago I
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
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
File IO
The Java IO package is used to perform various input/output processing... are packaged into java.io. package. File Management with the help... are planning to learn file management using Java IO package.This class is available
Java File Writing Example
Java File Writing Example To write some data on the file you need to first open the file in append mode by using the java.io.FileWriter class and then pass... data that have to be written on the file, as input. SampleInterfaceImp.java
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
Java Read Lines from Text File and Output in Reverse order to a Different Text File
to another text file. When that is done the output values of that file need...Java Read Lines from Text File and Output in Reverse order to a Different Text...); this.setContentPane(content); this.pack(); this.setTitle("File 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
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
Dialog and Console Input-Output
and file input. Of course, your program should eventually have a GUI user... Prev: Dialog Box Output | Next: Example: Captitalize Java NotesDialog and Console Input-Output  This is similar to the previous 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
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
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 and Output problems - Java Beginners
Input and Output problems  1) what is Difference between... to a particular platform. Thus, this class reads characters from a byte input stream... these values yourself, construct an InputStreamReader on a FileInputStream. FileReader
Counting Objects Clandestinely - Java Tutorials
Counting Objects Clandestinely 2001-12-28 The Java Specialists' Newsletter [Issue 038b] - Counting Objects Clandestinely - Follow-up Author: Dr. Heinz M... you use a "boolean counting" variable? A: I want to avoid counting objects
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
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
Reading And Writing Excel File
reading and writing excel file   ...; The output of the program is given below: Download this excel file. Download... org.apache.poi.poif.filesystem.POIFileSystem class is used to buffer the excel file into an object. In other
I/O Program output error
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... { // Prompts the user for the input file name (listings.txt) by creting
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
java word counting - Java Beginners
class ReplaceLetters{public static void main(String[] args){Scanner input;input... the original text line :the fat boy and his father output:the xxxxxx boy and his fatherwhile your program will output the following for the same inputs>
Java File Handling
input and output of bytes, byte stream is employed. For reading and writing... the required classes needed to perform input and output (I/O) in Java. Streams... In a file system, it gets input bytes from a file. FileOutputStream
File Handling - Java Beginners
{ //Create a new file output stream out = new FileOutputStream(str...) { System.err.println("Error writing to file...(); } catch (Exception e){ System.err.println("File input error
PHP File Manipulation File locking Tutorial
the file each time when you do any input/output operation with that file... from crashes when it is executing input/output operation. Modified code... will be useless. So it is highly recommended to use file locking anytime you execute input
PHP File Manipulation Writing to a File Tutorial
of the important work  is writing to a file, following example will illustrate how...; echo (fgets($file)); } ?> Output: In PHP, we can do many things... of the important work is writing to a file, following example will illustrate how
Java Jar File
for reading and writing the JAR (Java ARchive) file format, which is based... the contents of a JAR file to any output stream... Java Jar File      
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
Console Input: Scanner
Java NotesConsole Input: Scanner The java.util.Scanner class (added in Java 5) allows simple console and file input. Of course, your program should... 16 17 18 19 20 21 22 23 // File : introductory
Convert Text File to PDF file
BufferedReader(new FileReader( "**INPUT FILE**")); String inLine...(document, new FileOutputStream( "**OUTPUT FILE...Convert Text File to PDF file  Here is the way how to covert your
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"><
Counting the highest sequence of zeros and one's - Java Beginners
Counting the highest sequence of zeros and one's  Count the highest.... Input: You should ask user to enter any bit string with zeros and ones sequence. Output: It should contain the maximum sequence count of zeros and ones
Writing Calculator Stateless Session Bean
' bean. Writing JSP and Web/Ear component Our JSP file access the session bean...Writing Calculator Stateless Session Bean... Bean for multiplying the values entered by user. We will use ant build tool

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.