The simpler the program - seemingly the more scope for error :p
Hi again,
Just thought I'd check with you guys to see where I've gone wrong with this simple I/O program. Having just done quite a bit of reading on I/O I thought would write myself I nice little program from scratch to test some of the said principles out. Sadly my output is locked within an infinite loop when I try reading from it.
The first code dump is the main prog, it validates whether there is an output txt file created, and if not creates one. Thus on it's 2nd execution it will instantiate an OutputCheck object + invoke it's checkOutput() method to see whether the output txt file originally created fits the bill. But it's an endless loop as previously mentioned. X_X
Code Java:
import java.util.Scanner;
import java.io.*;
public class OutputCheck {
File checked = new File("src\\output.txt");
public void checkOutput(){
try{
Scanner scan = new Scanner(checked);
while(scan.hasNext()){
String str = scan.next();
System.out.println(str);
}
scan.close();
}catch(IOException e){
}finally{}
}
}
Re: The simpler the program - seemingly the more scope for error :p
Is this what you wanted?
Code Java:
import java.io.*;
public class StringFileReadWrite {
public static void main (String [] args){
File inFile = new File("src\\input.txt");
File outFile = new File("src\\output.txt");
if(!outFile.exists()){
try{
BufferedReader readInput = new BufferedReader(new FileReader(inFile));
PrintWriter writeOutPut = new PrintWriter(new BufferedWriter(new FileWriter(outFile)));
String line ="";
while((line = readInput.readLine()) != null) {
writeOutPut.println(line);
}
readInput.close();
writeOutPut.close();
System.out.println("The File Operation has been completed");
}catch(IOException e){
System.out.println(e);
}
} else{
OutputCheck oc = new OutputCheck();
oc.checkOutput();
}
}
}
Re: The simpler the program - seemingly the more scope for error :p
Quote:
Originally Posted by
bluurr
Is this what you wanted?
...
Ah, I like your efficiency with the anonymous classes instead of one big lump of chained code - definitely will keep a mental note of that cheers ;) However, no dice I'm afraid - I was more interested in the reason(s) behind my output being in an infinite loop. Any thoughts on that btw?
Re: The simpler the program - seemingly the more scope for error :p
Quote:
Code Java:
String line = br.readLine();
while(line != null){
pw.println(line);
br.readLine();
}
This will never terminate because you never change the value of line. Thus, unless on the first read line == null, you'll be stuck in an infinite loop.
Try running your program in debug mode and step through it one line of code at a time, or better yet pretend you're the computer and try running the code with a piece of paper to keep track of variable values.
Re: The simpler the program - seemingly the more scope for error :p
Quote:
Originally Posted by
helloworld922
This will never terminate because you never change the value of line. Thus, unless on the first read line == null, you'll be stuck in an infinite loop.
Try running your program in debug mode and step through it one line of code at a time, or better yet pretend you're the computer and try running the code with a piece of paper to keep track of variable values.
hmm, ok. So I realise that the condition of the while loop remains static - and thus will never terminate. But I'm not to sure of how to change this.
My first guess was to Change my code to this ( which seems more logical) but the condition still remains constant forever it would seem...
Code java:
while ((line = br.readLine()) != null) {
pw.println(line);
}
**This may be a bit of a shot in the dark, but I'm wondering if it has something to do with my input text file...
Re: The simpler the program - seemingly the more scope for error :p
Quote:
Originally Posted by
Bacon n' Logic
**This may be a bit of a shot in the dark, but I'm wondering if it has something to do with my input text file...
Having put a '\n' at the end of my input text file - the problem seems resolved - albeit with the '\n' now being visible on output. Is this the intended solution, Or is there another way?