Results 1 to 5 of 5
  1. #1
    Anarch is offline Member
    Join Date
    Apr 2015
    Posts
    7
    Rep Power
    0

    Default Such a simple program yet still it does not work

    Here it is:

    Java Code:
    import java.util.Scanner;
    
    class test {
    	
    	public static void main(String[] args) {
    		do {
    			Scanner scannerObj = new Scanner(System.in);
    		} while (scannerObj.nextLine() != "exit");
    	}
    	
    }
    This code alone doesn't do much. It's part of a larger project. I abstracted out the source of the problem because who needs all that noise. It is supposed to loop indefinitely until the user enters "exit" in the console. Thanks in advance to anyone who can help!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Such a simple program yet still it does not work

    Quote Originally Posted by Anarch View Post
    Such a simple program yet still it does not work
    Please go through the Forum Rules, particularly the third paragraph.

    Never use == or != to compare Strings or other reference types. Use the .equals(...) method instead.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Anarch is offline Member
    Join Date
    Apr 2015
    Posts
    7
    Rep Power
    0

    Default Re: Such a simple program yet still it does not work

    I looked at the forum rules like you requested. I cant imagine any way that anything in the third paragraph might have violated any of them. However perhaps the title did. I thought about that right after i posted it and tried to change the title but it wouldn't let me.

    thanks for the tip. Ill look into this .equals() method

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Such a simple program yet still it does not work

    Does the code compile? If not, and you don't understand the compiler's message, copy and post it.

  5. #5
    Anarch is offline Member
    Join Date
    Apr 2015
    Posts
    7
    Rep Power
    0

    Default Re: Such a simple program yet still it does not work

    Quote Originally Posted by pbrockway2 View Post
    Does the code compile? If not, and you don't understand the compiler's message, copy and post it.
    Thanks so much for taking an interest in my question but darryl's tip did the trick. This code:

    Java Code:
    import java.util.Scanner;
    
    class test {
    	
    	public static void main(String[] args) {
    		while (true) {
    			Scanner scannerObj = new Scanner(System.in);
    			if (scannerObj.nextLine().equals("exit")) {
    				break;
    			}
    		}
    	}
    	
    }
    Has the functionality I was looking for. I'll play around with it tomorrow to see if i can make it cleaner. I cant do != but i bet i can do !scannerObj.nextLine().equals("exit")
    Last edited by Anarch; 04-25-2015 at 07:50 AM.

Similar Threads

  1. Replies: 8
    Last Post: 09-09-2013, 03:48 PM
  2. Can't get this simple Calculator app to work!
    By postauma in forum New To Java
    Replies: 7
    Last Post: 02-18-2013, 08:50 AM
  3. What won't this simple code work?
    By xpl0rerchr in forum New To Java
    Replies: 3
    Last Post: 04-12-2012, 02:54 PM
  4. My Simple Array Does Not Work!
    By Simplev_v in forum New To Java
    Replies: 16
    Last Post: 09-07-2009, 02:43 PM
  5. Simple animation won't work
    By nolsen01 in forum New To Java
    Replies: 4
    Last Post: 07-08-2009, 11:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •