Results 1 to 4 of 4
  1. #1
    tkdvipers is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Smile terminating a while loop with a string

    Hi

    I am doing my nut and have come to the conclusion that you can't terminate a while loop using a String.

    For example

    while ( userInput != "end")
    {
    userInput = dialogbox("Please enter your choice")
    }

    Obviously the above code is just an example, however I am trying to get my code to put each input by the user into a set.

    I can compile the code and get the input from the user but when I input the string I have defined to terminate the loop, it just adds the input to the set. Do I have to format the string in a certain way??

    Thanks in advance to any help given.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    You question is not clear. You would better paste your actual code here and describe it there.

    Based on what you said here, i think you are comparing the string wrongly. A string variable is an object and you can't compare it with equals operator. You need to use equals method of it. (e.g. if (myString.equal("another string")))

  3. #3
    tkdvipers is offline Member
    Join Date
    Jul 2007
    Posts
    2
    Rep Power
    0

    Default

    Apologies for my question not being clear.

    Your answer helped a lot as I was trying to compare a user input from a request box to a string as you would with a primitive type, forgetting about strings being objects.

    The fact that I want each input to go into a set is not the issue, the issue is getting the while loop to terminate when the user enters *.

    JavaBean I am much obliged to you, many many thanks.
    Last edited by tkdvipers; 07-09-2007 at 10:47 PM.

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

Similar Threads

  1. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 12:56 AM
  2. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  3. i need assistance with a string triggered loop please!
    By Phobos0001 in forum New To Java
    Replies: 9
    Last Post: 11-14-2007, 02:44 PM
  4. Replies: 1
    Last Post: 08-03-2007, 10:52 AM
  5. A loop that doesn't loop
    By MichYer in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 08:44 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
  •