• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why this doesn't work??

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
He does not suffer fools gladly. But this tiny ad does:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic