Exiting While Loop After User Enters Value in String (Java)











up vote
0
down vote

favorite












Hello fellow StackOverflowers, I hope all of your days are going well.



I'm relatively new to Java programming and have found myself in a bit of pickle.



What I'm attempting to do is;




  1. Input Validation in Java - I want to make sure that the JOptionPane.showInput pane continues to re-appear (using a while loop) until the user has entered a value which is captured in the "this.accountName" String and;

  2. From there once the user has entered something in the JOptionPane.showInput pane I want to exit the loop and proceed to the other methods I have inside my OO program.


Unfortunately my while loop below exits after the first instance and doesn't continue in my code example below;



public String getAccountName() {
this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
if (this.accountName!= null) {
while (this.accountName != null) {
this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
if (this.accountName.contains("")){return this.accountName;
}
}
}
return this.accountName;
}


What would be the best way to go about fixing this?
I appreciate your help in advance!










share|improve this question







New contributor




Jaleel Spry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Might want to rethink this class. getters should not mutate the state of the instance.
    – flakes
    13 hours ago












  • This 'if (this.accountName.contains(""))' will always return true, unless accountName is null and in such case it will throw NullPointerException.
    – gawi
    13 hours ago










  • You should probably check if the dialog result is empty or just white space as well. Try this.accountName!= null && this.accountName.trim().length() != 0
    – Snake14
    4 hours ago















up vote
0
down vote

favorite












Hello fellow StackOverflowers, I hope all of your days are going well.



I'm relatively new to Java programming and have found myself in a bit of pickle.



What I'm attempting to do is;




  1. Input Validation in Java - I want to make sure that the JOptionPane.showInput pane continues to re-appear (using a while loop) until the user has entered a value which is captured in the "this.accountName" String and;

  2. From there once the user has entered something in the JOptionPane.showInput pane I want to exit the loop and proceed to the other methods I have inside my OO program.


Unfortunately my while loop below exits after the first instance and doesn't continue in my code example below;



public String getAccountName() {
this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
if (this.accountName!= null) {
while (this.accountName != null) {
this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
if (this.accountName.contains("")){return this.accountName;
}
}
}
return this.accountName;
}


What would be the best way to go about fixing this?
I appreciate your help in advance!










share|improve this question







New contributor




Jaleel Spry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Might want to rethink this class. getters should not mutate the state of the instance.
    – flakes
    13 hours ago












  • This 'if (this.accountName.contains(""))' will always return true, unless accountName is null and in such case it will throw NullPointerException.
    – gawi
    13 hours ago










  • You should probably check if the dialog result is empty or just white space as well. Try this.accountName!= null && this.accountName.trim().length() != 0
    – Snake14
    4 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Hello fellow StackOverflowers, I hope all of your days are going well.



I'm relatively new to Java programming and have found myself in a bit of pickle.



What I'm attempting to do is;




  1. Input Validation in Java - I want to make sure that the JOptionPane.showInput pane continues to re-appear (using a while loop) until the user has entered a value which is captured in the "this.accountName" String and;

  2. From there once the user has entered something in the JOptionPane.showInput pane I want to exit the loop and proceed to the other methods I have inside my OO program.


Unfortunately my while loop below exits after the first instance and doesn't continue in my code example below;



public String getAccountName() {
this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
if (this.accountName!= null) {
while (this.accountName != null) {
this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
if (this.accountName.contains("")){return this.accountName;
}
}
}
return this.accountName;
}


What would be the best way to go about fixing this?
I appreciate your help in advance!










share|improve this question







New contributor




Jaleel Spry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Hello fellow StackOverflowers, I hope all of your days are going well.



I'm relatively new to Java programming and have found myself in a bit of pickle.



What I'm attempting to do is;




  1. Input Validation in Java - I want to make sure that the JOptionPane.showInput pane continues to re-appear (using a while loop) until the user has entered a value which is captured in the "this.accountName" String and;

  2. From there once the user has entered something in the JOptionPane.showInput pane I want to exit the loop and proceed to the other methods I have inside my OO program.


Unfortunately my while loop below exits after the first instance and doesn't continue in my code example below;



public String getAccountName() {
this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
if (this.accountName!= null) {
while (this.accountName != null) {
this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
if (this.accountName.contains("")){return this.accountName;
}
}
}
return this.accountName;
}


What would be the best way to go about fixing this?
I appreciate your help in advance!







java string validation input while-loop






share|improve this question







New contributor




Jaleel Spry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Jaleel Spry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Jaleel Spry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 14 hours ago









Jaleel Spry

11




11




New contributor




Jaleel Spry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Jaleel Spry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Jaleel Spry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Might want to rethink this class. getters should not mutate the state of the instance.
    – flakes
    13 hours ago












  • This 'if (this.accountName.contains(""))' will always return true, unless accountName is null and in such case it will throw NullPointerException.
    – gawi
    13 hours ago










  • You should probably check if the dialog result is empty or just white space as well. Try this.accountName!= null && this.accountName.trim().length() != 0
    – Snake14
    4 hours ago


















  • Might want to rethink this class. getters should not mutate the state of the instance.
    – flakes
    13 hours ago












  • This 'if (this.accountName.contains(""))' will always return true, unless accountName is null and in such case it will throw NullPointerException.
    – gawi
    13 hours ago










  • You should probably check if the dialog result is empty or just white space as well. Try this.accountName!= null && this.accountName.trim().length() != 0
    – Snake14
    4 hours ago
















Might want to rethink this class. getters should not mutate the state of the instance.
– flakes
13 hours ago






Might want to rethink this class. getters should not mutate the state of the instance.
– flakes
13 hours ago














This 'if (this.accountName.contains(""))' will always return true, unless accountName is null and in such case it will throw NullPointerException.
– gawi
13 hours ago




This 'if (this.accountName.contains(""))' will always return true, unless accountName is null and in such case it will throw NullPointerException.
– gawi
13 hours ago












You should probably check if the dialog result is empty or just white space as well. Try this.accountName!= null && this.accountName.trim().length() != 0
– Snake14
4 hours ago




You should probably check if the dialog result is empty or just white space as well. Try this.accountName!= null && this.accountName.trim().length() != 0
– Snake14
4 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Use StringUtils.isBlank method (https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html) to check accountName value:



this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
while (StringUtils.isBlank(this.accountName)) {
this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
}
return this.accountName;





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Jaleel Spry is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349356%2fexiting-while-loop-after-user-enters-value-in-string-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Use StringUtils.isBlank method (https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html) to check accountName value:



    this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
    while (StringUtils.isBlank(this.accountName)) {
    this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
    }
    return this.accountName;





    share|improve this answer

























      up vote
      0
      down vote













      Use StringUtils.isBlank method (https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html) to check accountName value:



      this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
      while (StringUtils.isBlank(this.accountName)) {
      this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
      }
      return this.accountName;





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Use StringUtils.isBlank method (https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html) to check accountName value:



        this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
        while (StringUtils.isBlank(this.accountName)) {
        this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
        }
        return this.accountName;





        share|improve this answer












        Use StringUtils.isBlank method (https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html) to check accountName value:



        this.accountName = JOptionPane.showInputDialog(null, "Please enter a nick name for your new account (e.g. Savings Account)");
        while (StringUtils.isBlank(this.accountName)) {
        this.accountName = JOptionPane.showInputDialog(null, "Error! Please enter a valid name for your new account");
        }
        return this.accountName;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 13 hours ago









        Centos

        965




        965






















            Jaleel Spry is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Jaleel Spry is a new contributor. Be nice, and check out our Code of Conduct.













            Jaleel Spry is a new contributor. Be nice, and check out our Code of Conduct.












            Jaleel Spry is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349356%2fexiting-while-loop-after-user-enters-value-in-string-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin