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