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;
- 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;
- 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
New contributor
add a comment |
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;
- 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;
- 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
New contributor
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. Trythis.accountName!= null && this.accountName.trim().length() != 0
– Snake14
4 hours ago
add a comment |
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;
- 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;
- 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
New contributor
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;
- 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;
- 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
java string validation input while-loop
New contributor
New contributor
New contributor
asked 14 hours ago
Jaleel Spry
11
11
New contributor
New contributor
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. Trythis.accountName!= null && this.accountName.trim().length() != 0
– Snake14
4 hours ago
add a comment |
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. Trythis.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
add a comment |
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;
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered 13 hours ago
Centos
965
965
add a comment |
add a comment |
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.
Jaleel Spry is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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