dcsimg
BufferedWriter/BufferedReader/StreamTokenizer help required
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Maggi_Rose
Posted On:   Thursday, March 21, 2002 01:00 AM

Hello I have an assignment where I have to create a try statement that creates a BufferedReader and BufferedWriter assigned to particular file types - I have no problem with this. I then have to define a method called 'run' which uses a StreamTokenizer to processes the contents of the file. A '+' symbol has to be added in front of each of the numbers in the file read by BufferedReader and then a '=' and the total of the numbers be printed out. This is my code: import java.io.*; import java.util.*; import java.lang.*; public class Adder { public static BufferedWriter output; public static BufferedReader inp   More>>

Hello


I have an assignment where I have to create a try statement that creates a BufferedReader and BufferedWriter assigned to particular file types - I have no problem with this.


I then have to define a method called 'run' which uses a StreamTokenizer to processes the contents of the file. A '+' symbol has to be added in front of each of the numbers in the file read by BufferedReader and then a '=' and the total of the numbers be printed out.


This is my code:


import java.io.*;

import java.util.*;

import java.lang.*;


public class Adder {

public static BufferedWriter output;

public static BufferedReader input;

public static StreamTokenizer tok;


public Adder (BufferedWriter output, BufferedReader input, StreamTokenizer tok) {}


public static void main(String[] args) {


BufferedWriter output;

BufferedReader input;

StreamTokenizer tok;


try

{

File inputFile = new File("c:/JavaProjects/TMA01/Streams/MyInputFile.txt");

File outputFile = new File("c:/JavaProjects/TMA01/Streams/MyOutputFile.txt");


input = new BufferedReader (new FileReader(inputFile));

output = new BufferedWriter (new FileWriter(outputFile));

}

catch (Exception e) {System.exit(0);

}

}

public static void run (){

double total = 0;

StreamTokenizer tok = new StreamTokenizer(input);

try {

while (tok.nextToken()!=tok.TT_EOF) {
total = (total + tok.nval);

output.write("+ " + tok.nval);

}

int c;

output.write("= " + total);

output.flush();

output.close();

}catch (IOException e) {System.exit(0);}

}

}


I am obviously doing something very wrong as my output file is created but completely empty.


Can anyone point me in the right direction ?


Thanks


Maggi

   <<Less

Re: BufferedWriter/BufferedReader/StreamTokenizer help required

Posted By:   ricard_nacher  
Posted On:   Monday, April 1, 2002 01:44 PM

hi Maggi,


I think that your problem is refered to the area where a variable cold be referencied. In other words, in your main method, you are initialazing these local(main method)variables


BufferedWriter output;<---- initialazing


BufferedReader input;<-----initialazing


...


input = new BufferedReader (new FileReader(inputFile));


output = new BufferedWriter (new FileWriter(outputFile));


--------


but in the run method you are using the class fields


public static BufferedWriter output;

public static BufferedReader input;

public static StreamTokenizer tok;


whitch you have not initialized.


I think you should not declare any variable inside your main with the same name as a class field, if you want to use these variables as globals


pd:I'm sorry for my english, i hope it can help!

About | Sitemap | Contact