WHILE problems

My program reads a file, converts each line to uppercase and then adds each line to a vector. Its meant to exit the program if no STOP statement is found in the whole file. If a STOP statement is found it should go on to the next part of the program which reads each line of the file and performs if statements on them.

On the file I'm testing it with, there is a STOP statement on the last line. It finds this STOP statement (as it returned 10 on the last line). But "no STOP statement" is still printed and the program doesn't run on to the next while loop.

Any ideas?!

while (( line=file.readLine() ) !=null)

{

countE++;

lineUp = line.toUpperCase();

lines.addElement(lineUp);

}

if (lines.indexOf("STOP") == -1);

{

System.out.println("ERROR [line "+countE+"]: no STOP statement");

System.exit(0);

}

while (( line=file.readLine() ) !=null)

{

linecount++;

[1337 byte] By [computersaysnoa] at [2007-11-26 12:34:29]
# 1
The stop line do probably have some spaces after or before the command. That is, you have not added "STOP" to the list, but rather "STOP " or something similar.Kaj
Kaja at 2007-7-7 15:50:02 > top of Java-index,Java Essentials,New To Java...
# 2

OP, Your question is not clear. You need something like this

BufferedReader br = new BufferedReader(new FileReader("C:\\input.txt"));

String str = null;

Vector v = new Vector();

boolean stop = false;

while ((str = br.readLine()) != null) {

if(str.indexOf("STOP") != -1) {

stop = true;

break;

}

else v.add(str);

}

br.close();

if(stop) {

System.out.println("STOP Found!");

System.exit(0);

}

else {

System.out.println("STOP Not Found!");

System.out.println(v);

}

Cheers

astelaveestaa at 2007-7-7 15:50:02 > top of Java-index,Java Essentials,New To Java...
# 3

Try using

// Make sure equals() in indexOf() compares the Strings lexicographically

Vector<String> lines = new Vector<String>();

and

// Strip leading and trailing whitespace

lines.addElement(line.toUpperCase().trim());

Jukka

duckbilla at 2007-7-7 15:50:02 > top of Java-index,Java Essentials,New To Java...