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
Login         

View Questions and Answers by Category

Advertisements


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


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

 

 

DMCA.com