473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why does not my Do-While work...

Hi,
This program prints Hi and ask the user "Print again" if user types"yes" the program should priny "Hi" and ask the question again.
This is the code I wrote,for some reason the program does nothing when the user enter "yes",Please fix the problem for me.
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class sample
  3. {
  4.     public static void main(String [] args)
  5.     {
  6.      String again;   
  7. do{
  8.         System.out.println("Hello");
  9.         Scanner in=new Scanner(System.in);
  10.         System.out.println("Print  again???");
  11.         again=in.next();
  12. }
  13. while(again=="yes");
  14. }
  15. }
my friend said I should change while(again=="yes") to again.equals("yes")
Why should I do that?Why does not java like again=="yes"???
Oct 15 '09 #1
2 2086
pbrockway2
151 Expert 100+
@alireza6485
Your friend is right.

The .equals() does something well documented: it returns true if and only if the two strings are made up of the same characters in the same order. This is exactly what you want and so you should use that method.

== does something different. It is true if and only if the two references it is comparing have the same value. References have the same value when they refer (point to) the same object. In the case of strings it should be clear that you can have different strings made up of the same characters in the same order. (As an example look at the 5th and the 15th words in the previous sentence. One is near the start of the sentence, one is in the middle. They are clearly not "the same" in the sense of identical. But they are made up of the same characters in the same order.).

An expression like new String("whatever"); will always return a reference to a string that is not identical to any others. (That's what "new" means). But the string it refers (points) to may well be equal() to others.
Oct 15 '09 #2
Yene
13
import java.util.Scanner;
public class swing
{
public static void main(String [] args)
{
String again;
do{
System.out.println("Hello");
Scanner in=new Scanner(System.in);
System.out.println("Print again???");
again=in.next();
}
while(again.equals("yes"));
}
}
This should work since == operator checks to see if two objects are exactly the same object.
Oct 18 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
31
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the...
5
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to...
10
by: Mr Newbie | last post by:
DropDown lists and Listboxes do not appear in the list of controls and values passed back to the server on PostBack in Request.Form object. Can someone confirm this to be correct and possibly...
7
by: Daniel Rudy | last post by:
Hello, I have a peice of code that I'm making an attempt to code. The problem is that I need to return an arbitrary number of char strings. int function(char *fname, int *dcount, char *data)...
16
by: lawrence k | last post by:
I've a file upload script on my site. I just now used it to upload a small text document (10k). Everything worked fine. Then I tried to upload a 5.3 meg Quicktime video. Didn't work. I've...
38
by: ssg31415926 | last post by:
I need to compare two string arrays defined as string such that the two arrays are equal if the contents of the two are the same, where order doesn't matter and every element must be unique. ...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
37
by: Erick-> | last post by:
hello!! I was looking at some code in C... and saw this "exotic" operator |=. first time with it, what exactky does??? thanks in advance. Erick->
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.