Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

Have Programming Question? Ask it here!
 
 
 


sam
counting the values in input file and and writing the output to a file
2 Answer(s)      2 years 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(); } }


Advertisement
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
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
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
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
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 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
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
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
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 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
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 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
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. 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
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
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 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
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"><
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
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
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
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
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
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
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
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
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
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
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
reading dropdown values from properties file in jsp
reading dropdown values from properties file in jsp  reading dropdown values from properties file in jsp
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...) This is the simplest example of the class used for writing character file. BufferedWriter... are used for writing Character files and Text Files respectively while another class
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
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
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
how to save an input to jsp page in a text file?ave an input given b
how to save an input to jsp page in a text file?ave an input given b  how to save the input given by the user in jsp page to a text file
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
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and 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
How to get the full path location using <input type=file>
How to get the full path location using input type=file  Hi. I have used input type=file> in HTML to select an image. But it selects only the image but not with its full path location. how to do it? Thanks in advance
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
Input / Output in Easiest Method in Java
Input / Output in Easiest Method in Java  How to input from keyboard, both strings and characters and display the result on the screen...)throws Exception{ Scanner input=new Scanner(System.in
 

 

 

DMCA.com