Java while loop not repeating
up vote
0
down vote
favorite
I am writing this code where the user puts the username and password then I validate the entries in a database. The problem with my code is that when the input is wrong it does not return and repeat the data input process from the while loop. My code is as below. If someone could help me fix it. Thank you in advance.
boolean b = true;
while (b == true)
System.out.println("enter username");
String username = scanner.next();
System.out.println("enter password");
String password= scanner.next();
boolean result = userDao.validate(username, password);
if (result == false)
System.out.println("password email do not match");
scanner.next();
else
System.out.println("success");
b = false;
java loops validation while-loop
add a comment |
up vote
0
down vote
favorite
I am writing this code where the user puts the username and password then I validate the entries in a database. The problem with my code is that when the input is wrong it does not return and repeat the data input process from the while loop. My code is as below. If someone could help me fix it. Thank you in advance.
boolean b = true;
while (b == true)
System.out.println("enter username");
String username = scanner.next();
System.out.println("enter password");
String password= scanner.next();
boolean result = userDao.validate(username, password);
if (result == false)
System.out.println("password email do not match");
scanner.next();
else
System.out.println("success");
b = false;
java loops validation while-loop
3
Removescanner.next()
from the if statement?
– Maarten Bodewes
Nov 10 at 0:02
What is the purpose ofscanner.next();
afterSystem.out.println("password email do not match");
?
– Pshemo
Nov 10 at 0:04
2
If you're using an IDE like Eclipse or IntelliJ, you can figure this out by using the debugger. Let the code run and pause the debugger when it's stuck. If it's not stuck, you can also step through line by line to see what's going on.
– kichik
Nov 10 at 0:05
hello!, Did you see wether in your userDao.validate() crash something? i mean, are you sure that result have a value from this method?, maybe don't get back result data.
– Daniel Carreto
Nov 10 at 0:20
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am writing this code where the user puts the username and password then I validate the entries in a database. The problem with my code is that when the input is wrong it does not return and repeat the data input process from the while loop. My code is as below. If someone could help me fix it. Thank you in advance.
boolean b = true;
while (b == true)
System.out.println("enter username");
String username = scanner.next();
System.out.println("enter password");
String password= scanner.next();
boolean result = userDao.validate(username, password);
if (result == false)
System.out.println("password email do not match");
scanner.next();
else
System.out.println("success");
b = false;
java loops validation while-loop
I am writing this code where the user puts the username and password then I validate the entries in a database. The problem with my code is that when the input is wrong it does not return and repeat the data input process from the while loop. My code is as below. If someone could help me fix it. Thank you in advance.
boolean b = true;
while (b == true)
System.out.println("enter username");
String username = scanner.next();
System.out.println("enter password");
String password= scanner.next();
boolean result = userDao.validate(username, password);
if (result == false)
System.out.println("password email do not match");
scanner.next();
else
System.out.println("success");
b = false;
java loops validation while-loop
java loops validation while-loop
edited Nov 10 at 0:56
Hovercraft Full Of Eels
260k20210316
260k20210316
asked Nov 9 at 23:57
fdgh
1
1
3
Removescanner.next()
from the if statement?
– Maarten Bodewes
Nov 10 at 0:02
What is the purpose ofscanner.next();
afterSystem.out.println("password email do not match");
?
– Pshemo
Nov 10 at 0:04
2
If you're using an IDE like Eclipse or IntelliJ, you can figure this out by using the debugger. Let the code run and pause the debugger when it's stuck. If it's not stuck, you can also step through line by line to see what's going on.
– kichik
Nov 10 at 0:05
hello!, Did you see wether in your userDao.validate() crash something? i mean, are you sure that result have a value from this method?, maybe don't get back result data.
– Daniel Carreto
Nov 10 at 0:20
add a comment |
3
Removescanner.next()
from the if statement?
– Maarten Bodewes
Nov 10 at 0:02
What is the purpose ofscanner.next();
afterSystem.out.println("password email do not match");
?
– Pshemo
Nov 10 at 0:04
2
If you're using an IDE like Eclipse or IntelliJ, you can figure this out by using the debugger. Let the code run and pause the debugger when it's stuck. If it's not stuck, you can also step through line by line to see what's going on.
– kichik
Nov 10 at 0:05
hello!, Did you see wether in your userDao.validate() crash something? i mean, are you sure that result have a value from this method?, maybe don't get back result data.
– Daniel Carreto
Nov 10 at 0:20
3
3
Remove
scanner.next()
from the if statement?– Maarten Bodewes
Nov 10 at 0:02
Remove
scanner.next()
from the if statement?– Maarten Bodewes
Nov 10 at 0:02
What is the purpose of
scanner.next();
after System.out.println("password email do not match");
?– Pshemo
Nov 10 at 0:04
What is the purpose of
scanner.next();
after System.out.println("password email do not match");
?– Pshemo
Nov 10 at 0:04
2
2
If you're using an IDE like Eclipse or IntelliJ, you can figure this out by using the debugger. Let the code run and pause the debugger when it's stuck. If it's not stuck, you can also step through line by line to see what's going on.
– kichik
Nov 10 at 0:05
If you're using an IDE like Eclipse or IntelliJ, you can figure this out by using the debugger. Let the code run and pause the debugger when it's stuck. If it's not stuck, you can also step through line by line to see what's going on.
– kichik
Nov 10 at 0:05
hello!, Did you see wether in your userDao.validate() crash something? i mean, are you sure that result have a value from this method?, maybe don't get back result data.
– Daniel Carreto
Nov 10 at 0:20
hello!, Did you see wether in your userDao.validate() crash something? i mean, are you sure that result have a value from this method?, maybe don't get back result data.
– Daniel Carreto
Nov 10 at 0:20
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
I don't have enough "reputation points" to provide a comment.. but curious if you would be able to remove scanner.next() within your if statement if that would do the trick.
I tried it by setting the result to false and letting it run.
if (result == false)
System.out.println("password email do not match");
//scanner.next();
else
System.out.println("success");
b = false;
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I don't have enough "reputation points" to provide a comment.. but curious if you would be able to remove scanner.next() within your if statement if that would do the trick.
I tried it by setting the result to false and letting it run.
if (result == false)
System.out.println("password email do not match");
//scanner.next();
else
System.out.println("success");
b = false;
add a comment |
up vote
1
down vote
I don't have enough "reputation points" to provide a comment.. but curious if you would be able to remove scanner.next() within your if statement if that would do the trick.
I tried it by setting the result to false and letting it run.
if (result == false)
System.out.println("password email do not match");
//scanner.next();
else
System.out.println("success");
b = false;
add a comment |
up vote
1
down vote
up vote
1
down vote
I don't have enough "reputation points" to provide a comment.. but curious if you would be able to remove scanner.next() within your if statement if that would do the trick.
I tried it by setting the result to false and letting it run.
if (result == false)
System.out.println("password email do not match");
//scanner.next();
else
System.out.println("success");
b = false;
I don't have enough "reputation points" to provide a comment.. but curious if you would be able to remove scanner.next() within your if statement if that would do the trick.
I tried it by setting the result to false and letting it run.
if (result == false)
System.out.println("password email do not match");
//scanner.next();
else
System.out.println("success");
b = false;
answered Nov 10 at 0:10
PMoe
265
265
add a comment |
add a comment |
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%2f53234769%2fjava-while-loop-not-repeating%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
3
Remove
scanner.next()
from the if statement?– Maarten Bodewes
Nov 10 at 0:02
What is the purpose of
scanner.next();
afterSystem.out.println("password email do not match");
?– Pshemo
Nov 10 at 0:04
2
If you're using an IDE like Eclipse or IntelliJ, you can figure this out by using the debugger. Let the code run and pause the debugger when it's stuck. If it's not stuck, you can also step through line by line to see what's going on.
– kichik
Nov 10 at 0:05
hello!, Did you see wether in your userDao.validate() crash something? i mean, are you sure that result have a value from this method?, maybe don't get back result data.
– Daniel Carreto
Nov 10 at 0:20