Computer problems,Computer help
*AX SOFT>>>Programming & Design

How to terminate a Java while loop using a string?


import java.util.*;
public class Loopy
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
String str = "ok";
String output;
while (str != "done")
{
System.out.print ("Enter a word: ");
str = console.next();
System.out.print ("Your word is: " + str);
}
System.out.print ("You input done.");
}
}


When the user inputs done it stays in the loop, any ideas?

Your while condition will always evaluate to true. The expression

str != "done"

asks whether the reference str is different from the reference "done" - that is, do these items occupy different memory addresses? For String, == always compares by reference, not by value. For compare-by-value, which is your intent for your while condition, you should replace

str != "done"

with

!"done".equals(str)

or

!str.equals("done")

You could try using the String.equalsIgnoreCase() or String.compareTo() methods.

!= and == don't work the way you'd expect with String. You need String.equals() or String.equalsIgnoreCase().

the problem is that you are not using object comparision. != will work only for primitives.

Try replace the while condition with

while(! ("done").equals(str))

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • How do you automatically load a javascipt?

    Put the javascript in a file and in a subfolder of your project and then reference it in the header Example: <script language="javascript" src="scripts/general.js"><...

  • Is there any basic principles or codes needed to design a web page?

    Checkout AceHtmlPro (Free Edition) which will help you learn html and allows for easy page generation. Great for anyone starting out.

    ...
  • Dreamweaver MX: Why has the background & links disappeard on my 3rd page when I link to it from my homepage?

    Where exactly are you storing the specification of your links? In each page? Or in a separate style sheet (CSS)? If you're not using an external style sheet, I highly recommend starting to do ...

  • Unix help!!?

    Files in Unix filesystems use inodes as structures that hold the actual file data on disks. inode number uniquely identifies a physical file on disk. Hard links are pretty much multiple names fo...

  • Entering words in to C++?

    It's as simple as this, in C++: string s; cin >> s;

    ...
  • Does Gimp work on Vista?

    GIMP should run on any NT-based version of Windows (NT4, 2000, XP or Vista). Older GIMP versions (2.0.x) could also work with older versions of Windows such as Windows 95 but they are not supported...

  • How can I have a link to a flash file (.swf) open in a new window on my website?

    When you link to the .swf you can use target="blank" in the href.... <A HREF="link.swf" target="_blank"> This will force the page to open in a new window.....

  • Error C4430: missing type specifier - int assumed. Note: C++ does not support default-int?

    main() ...should be... int main(void) ...aside from that, you should probably work on understanding how "else if" works.

    ...
  •  

    Categories--Copyright/IP Policy--Contact Webmaster