Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 04:55 AM
Member
 
Join Date: Jul 2007
Posts: 40
toby is on a distinguished road
How to use BufferredReader
Hi, I am attempting to input integer date from a text document, do a few calculation with the data, and output it to html.
The problem is I can even get the BufferredReader to input the file to make sure my loops are working. Here is the code:
Code:
import java.util.Scanner; import java.io.*; import java.text.DecimalFormat; public class Lab7 { public static void main(String[] args) { try { System.out.print("Enter the input filename: ");//open file BufferedReader inputFile = new BufferedReader(new InputStreamReader(System.in)); String str = inputFile.readLine(); BufferedReader input = new BufferedReader(new FileReader(str)); System.out.print("Enter the output filename: "); BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); String str1 = userInput.readLine(); PrintWriter outputFile = new PrintWriter(new FileOutputStream(str1));//create output file int numberOfSeries = Integer.parseInt(input.readLine()); for (int i = 0; i < numberOfSeries; i = i + 1) { int maximum = 0, minimum = 99999; int numberOfValues = Integer.parseInt(input.readLine()); int [] dataSpread = new int [numberOfValues]; for (int t = 0; numberOfValues > t; numberOfValues = numberOfValues - 1) { int dataValue = Integer.parseInt(inputFile.readLine()); if (dataValue > maximum) dataValue = maximum; if (dataValue < minimum) dataValue = minimum; dataValue = dataSpread[numberOfValues - 1]; } } } catch (IOException e) { System.out.println("Error occured, please restart application."); } } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-25-2007, 04:44 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
Your problem is that you are creating an extra BufferedReader if you remove the creation of userInput and re-use the inputFile variable the bufferedReader should work better.

Also you want to add a finally block to ensure the readers are all closed at the end.

Here is one I have edited for you:

Code:
BufferedReader userInput = null; BufferedReader input = null; PrintWriter outputFile = null; try { System.out.print("Enter the input filename: ");// open file userInput = new BufferedReader(new InputStreamReader(System.in)); String str = userInput.readLine(); input = new BufferedReader(new FileReader(str)); System.out.print("Enter the output filename: "); String str1 = userInput.readLine(); outputFile = new PrintWriter(new FileOutputStream(str1));// create // output // file int numberOfSeries = Integer.parseInt(input.readLine()); for (int i = 0; i < numberOfSeries; i = i + 1) { int maximum = 0, minimum = 99999; int numberOfValues = Integer.parseInt(input.readLine()); int[] dataSpread = new int[numberOfValues]; for (int t = 0; numberOfValues > t; numberOfValues = numberOfValues - 1) { int dataValue = Integer.parseInt(userInput.readLine()); if (dataValue > maximum) dataValue = maximum; if (dataValue < minimum) dataValue = minimum; dataValue = dataSpread[numberOfValues - 1]; } } } catch (IOException e) { System.out.println("Error occured, please restart application."); } finally { try { // close all the readers. input.close(); userInput.close(); outputFile.flush(); outputFile.close(); } catch (Exception e) { e.printStackTrace(); } }
Hope this helps.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 05:20 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org