File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes Why this doesn't work?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Applets
Reply Bookmark "Why this doesn Watch "Why this doesn New topic
Author

Why this doesn't work??

Asleshkumar Thukaram
Greenhorn

Joined: Jan 25, 2001
Posts: 9
import java.io.*;
class brr {
public static void main(String args[])
throws IOException
{
BufferedReader br = newBufferedReader(new InputStreamReader(System.in));
String c;
System.out.println("enter lines, 'stop' to quit ");
do{
c=br.readLine();
System.out.println(c);
} while(c!="stop");
}
}
When executing this pgm, it doesn't stop even when i enter 'stop', but the same pgm works if i use the following statement
"while (!c.equals("stop"));" instead of "while(c!="stop");". Logically I thought that both the statements were same, but they don't work. Please explain.
Thanx,
Aslesh
sujata mehta
Greenhorn

Joined: Feb 21, 2001
Posts: 4
hi!
Actually u r using c as a string so u have to use String comparison method .equals to compare the two values .but when u r comparing as c!="stop" its not comparing the two string's data but as two objects hence its value is not true & the program doesn't stop.
 
 
subject: Why this doesn't work??
 
Threads others viewed
need help
InputStreamReader
Nested while loop inside do...while loop?
String
Issue First JDBC Pgm
developer file tools