why the following code cannot run out of the while loop?...thank you

M

mike

String line;
boolean headerflag=true;
boolean HTMLflag=false;
boolean =true;

do{
line=br.readLine();

if(line.length()!=0 && headerflag==true && HTMLflag==false){
fwHTTPHeader.write(line+"\r\n");
}

else if(line.length()==0 && headerflag==true && HTMLflag==false){
fwHTTPHeader.write(line+"\r\n");
headerflag=false;
HTMLflag=true;
fwHTTPHeader.close();
}
else if(line.length()==0 && headerflag==false && HTMLflag==true){
fw.write(line+"\r\n");
}
else {
fw.write(line+"\r\n");
}

System.out.println("test");

}
while(line!=null);
 
P

Paul Lutus

mike said:
String line;
boolean headerflag=true;
boolean HTMLflag=false;
boolean =true;

do{
line=br.readLine();

if(line.length()!=0

You test the "line" variable for null much later in your code. Because it
might be null here, this line will fail when it is. This same error is
repeated through your code.

Do it this way:

while((line=br.readLine()) != null) {
// all your code here
}
 
G

Gordon Beaton

do{
line=br.readLine();
[...]

}
while(line!=null);

Please _format_ your code before posting it...

line won't be null until the BufferedReader reaches the end of the
input stream. If the underlying structure is a socket connection, that
won't happen until the remote _closes_ his end of the connection.

Note too that you'll get a NullPointerException when EOF is reached,
because in the entire loop body you've assumed that line is not null,
and the test is not done until afterwards. Write the loop like this
instead:

while ((line = br.readLine()) != null) {
[...]
}

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top