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;











share|improve this question



















  • 3




    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






  • 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














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;











share|improve this question



















  • 3




    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






  • 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












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;











share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 0:56









Hovercraft Full Of Eels

260k20210316




260k20210316










asked Nov 9 at 23:57









fdgh

1




1







  • 3




    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






  • 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




    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






  • 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












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;






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
    );



    );













     

    draft saved


    draft discarded


















    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

























    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;






    share|improve this answer
























      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;






      share|improve this answer






















        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;






        share|improve this answer












        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;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 0:10









        PMoe

        265




        265



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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

            Darth Vader #20

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Ondo