Results 1 to 2 of 2
  1. #1
    toby is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default 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:
    Java 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

  2. #2
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    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:

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •