1

I'm trying to write a method for requesting user input via dialog box multiple times which only stops requesting when an empty string is entered.

Each entry needs to be added to a list, which I'll call aList. I'm using a bespoke import for the dialog box in my program which you may not be familiar with called Dialog.request("");.

I dont necessarily have to use this but I do need a pop up dialog box which only closes when an empty string is entered or cancel is pressed. Hopefully you can help

public void addToList()

{
    String inputName = Dialog.request("Please Enter A Name");
    aList.add(inputName);
}

With the while loop it is as follows

public void addToList()
{   String inputName = Dialog.request("Please Enter A Name");
    while(inputName !=""){
    aList.add(inputName);
}
}

I tried to implement a while loop but that lead to the following error

java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError at unknown line

Any tips are appreciated. So far invoking the method with x.addToList(); only adds one entry at a time. When I put in the while loop or do-while loop I get the error

12
  • 3
    From the Error, it seems very likely that you have created an infinite loop. Make sure the loop terminates. Also, you should always make sure to include all relevant code in your question, in this case the loop that produces the Error Commented May 6, 2015 at 15:08
  • I'll give it a shot, thanks. Should I be using a while loop or a do-while loop? Commented May 6, 2015 at 15:09
  • If you know (and can guarantee) there will always be at least one operation, a do-while is appropriate Commented May 6, 2015 at 15:10
  • 1
    @JackB please post full while loop code... Commented May 6, 2015 at 15:11
  • I have added it in my edit. Commented May 6, 2015 at 15:19

3 Answers 3

1

There are a couple problems here. First off when comparing Strings/Objects you should use .equals or .equalsIgnoreCase not != or ==. The next problem is you aren't re-prompting a user for input inside your loop. You might want something like this:

public void addToList()
{
    String inputName = Dialog.request("Please Enter A Name");
    while(!inputName.equalsIgnoreCase(""))
    {
        aList.add(inputName);
        inputName = Dialog.request("Please Enter A Name");
    }
}

If the user enters a name you should keep asking them for another name until they don't enter a name.

Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is here:

while(inputName !=""){
    aList.add(inputName);
}

There are 2 issues with this. First you cannot compare strings with != or ==, as this will instead compare the references. User String.equals() instead.

Second, you are checking against a variable that does not change. This is critical because it means that the condition in the loop will either always or never be true. You must change the condition at some point within the loop or else it will never terminate.

The easiest way you can do this is by simply requesting user input within the loop like so:

String inputName = "nothing";
while(!inputName.equals("")){
    inputName = Dialog.request("Please Enter A Name");
    aList.add(inputName);
}

Comments

1

As an equal - but in my opinion more elegant - alternative to previous answers, the loop could be expressed with a do-while statement:

String inputName;
do {
    inputName = Dialog.request("Please Enter A Name");
    if (!inputName.equals("")) {
        aList.add(inputName);
    }
} while (!inputName.equals(""));

3 Comments

I'll check that that works as well for future reference, thanks for your input
yeah that works great as well, I would upvote you if I weren't but a lowly SO noob
I just gave your question an upvote. Your noobness should decrease a bit :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.