Results 1 to 4 of 4
  1. #1
    xlactyu is offline Member
    Join Date
    Oct 2014
    Posts
    9
    Rep Power
    0

    Default Why isn't my WHILE loop working?

    I am trying to make this program to keep repeating itself until I type "hello". Nothing useful just seeing if I could do it, and apparently not.

    Java Code:
    import java.util.Scanner;
    
    class comparingstrings
    {
       public static void main (String args [])
       { 
          Scanner input = new Scanner(System.in);
          String hello;
          hello = "hello";
          int tries = 0;
       
          while(! hello.equals ("hello"));
          {
             
             System.out.println("Type in a string");
             hello = input.nextLine();
            
             if (hello.equals("bye"))
             {
                System.out.println("Done");
             }
             else 
             {
                if (! hello.equals("bye"));
                {
                   tries = (tries + 1);
                }
             
             }
          }
       }
    }
    After I type and press enter one time the program ends, even if I don't type hello. Help me please.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why isn't my WHILE loop working?

    Your while loop says, "while hello is not equal to 'hello'" do the loop. So it enters
    the loop and you enter a some input (even a return). So the variable hello has changed.
    It is no longer equal to 'hello'. It is not equal to 'bye' so you increment tries and then exit
    the while loop.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Why isn't my WHILE loop working?

    That semicolon at the end of line #12 should go.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: Why isn't my WHILE loop working?

    another issue is there..u have initialized hello and checking != hello condition..stating itself it will not enter into the loop. make

    hello = ""; then test
    Ramya:cool:

Similar Threads

  1. Simple loop within a loop not working.
    By supremegrandruler in forum New To Java
    Replies: 3
    Last Post: 03-21-2014, 09:27 AM
  2. Replies: 10
    Last Post: 11-23-2013, 06:47 PM
  3. My while loop is not working, why??
    By esined93 in forum New To Java
    Replies: 3
    Last Post: 10-24-2012, 01:05 AM
  4. for loop not working?
    By Newbieprogrammer in forum New To Java
    Replies: 3
    Last Post: 07-24-2012, 02:55 PM
  5. while loop not working
    By RBNSN83 in forum New To Java
    Replies: 6
    Last Post: 06-21-2010, 07:29 AM

Posting Permissions

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