Hi I'm writing my first Java app and I've got a few Testcases (*.tc files) I want to direct to the app via this script:
for f in `ls *.tc`; do
echo "Current Testcase: $f"
x=${f%.*}
java Main < $x.tc > $x.out
if diff "$x.out" "$x.should"; then
echo "passed testcase $f"
let PASSED=PASSED+1
else
echo "failed testcase $f"
let FAILED=FAILED+1
fi
done
The Problem is I can't quite figure out why as soon as the tc file
contains more than one line the app goes nuts. For example: quit.tc
contains
quit
and works just like when I manually enter "quit", therfore the testcase passes.
However when I write another tc: quit2.tc
which contains
lala
test
quit
The app quits after the first command (because the readString function seems to return null afterwards). Here is the function responsible for reading:
public String readString(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String answer = null;
try {
answer = br.readLine();
return answer;
}
catch(IOException ioe) {
System.out.println("IO Error");
}
return answer;
}
I dont know why or when this function returns null when I redirect to the app, which seems to be the problem. Can you help out so I can get the tc script working? thx
If you're new to java, and you still shape your style and way of doing things, I will recommend you 2 tips:
1) use Scanner to read your input. Using nextLine() method of the Scanner object is probably what you're looking for.
2) design your code, so that it's testable via JUnit.
Still, in your readString()
method, remove the return answer;
from the try block.
UPDATE: try to implement the following in your function:
a) while the scanner instance hasNextLine() is true ->
b) call scanner instance nextLine() method ->
c) parse the line, and see if it equals 'quit' ->
d) implement the corresponding logical if
cases.