Duplicate scanner band-aid for do-while? (Java)












1















So I'm a little confused as to why this is happening, here's my code:



public static void main (String args)
{
Scanner kb = new Scanner(System.in);

do
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a time in 24-hour notation: ");
String time = in.nextLine();

int colonIndex = time.indexOf(":");
int hours = Integer.parseInt(time.substring(0, colonIndex));
int minutes = Integer.parseInt(time.substring(colonIndex + 1));

boolean legalTime = ((hours < 24 && hours > 0) && (minutes < 60 && minutes >= 0));
boolean addZero = minutes < 10;
boolean pm = hours > 12;

if(legalTime)
{
if(pm && addZero)
{
int newHour = hours - 12;
System.out.printf("That is the same as"
+ "n%d:0%d PMn", newHour, minutes);
}
else if(pm && !addZero)
{
hours = hours - 12;
System.out.printf("That is the same as"
+ "n%d:%d PMn", hours, minutes);
}
else if (!pm && addZero)
{
System.out.printf("That is the same as"
+ "n%d:0%d AMn", hours, minutes);
}
else
{
System.out.printf("That is the same as"
+ "n%d:%d AMn", hours, minutes);
}
}
try
{
if(!legalTime)
{
throw new Exception("Exception: there is no such time as " + time);
}
}
catch(Exception e)
{
System.out.println(e.getMessage()
+ "nAgain? (y/n)");
continue;
}

System.out.println("Again? (y/n)");

}while(Character.toUpperCase(kb.next().charAt(0)) == 'Y');

}


My code itself is not the issue, it's that the condition of a do-while loop only recognizes booleans outside of the do-while, which in my mind makes it quite frustrating to make the condition be affected by whatever is inside the block. What I want to do is make my code run, and then ask the user if they want to run it again, denoted by "y" or "n." I can't put



!time.charAt(0) == 'y'


As the condition because the string "time" is defined inside the do-while loop, so I made some weird band-aid by using one scanner just for the condition input before the do-while begins, and using another one inside the body. I know this is bad, but I can't think of a simple way to create a boolean for this condition that isn't inside the do-while loop, am I missing something?










share|improve this question



























    1















    So I'm a little confused as to why this is happening, here's my code:



    public static void main (String args)
    {
    Scanner kb = new Scanner(System.in);

    do
    {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter a time in 24-hour notation: ");
    String time = in.nextLine();

    int colonIndex = time.indexOf(":");
    int hours = Integer.parseInt(time.substring(0, colonIndex));
    int minutes = Integer.parseInt(time.substring(colonIndex + 1));

    boolean legalTime = ((hours < 24 && hours > 0) && (minutes < 60 && minutes >= 0));
    boolean addZero = minutes < 10;
    boolean pm = hours > 12;

    if(legalTime)
    {
    if(pm && addZero)
    {
    int newHour = hours - 12;
    System.out.printf("That is the same as"
    + "n%d:0%d PMn", newHour, minutes);
    }
    else if(pm && !addZero)
    {
    hours = hours - 12;
    System.out.printf("That is the same as"
    + "n%d:%d PMn", hours, minutes);
    }
    else if (!pm && addZero)
    {
    System.out.printf("That is the same as"
    + "n%d:0%d AMn", hours, minutes);
    }
    else
    {
    System.out.printf("That is the same as"
    + "n%d:%d AMn", hours, minutes);
    }
    }
    try
    {
    if(!legalTime)
    {
    throw new Exception("Exception: there is no such time as " + time);
    }
    }
    catch(Exception e)
    {
    System.out.println(e.getMessage()
    + "nAgain? (y/n)");
    continue;
    }

    System.out.println("Again? (y/n)");

    }while(Character.toUpperCase(kb.next().charAt(0)) == 'Y');

    }


    My code itself is not the issue, it's that the condition of a do-while loop only recognizes booleans outside of the do-while, which in my mind makes it quite frustrating to make the condition be affected by whatever is inside the block. What I want to do is make my code run, and then ask the user if they want to run it again, denoted by "y" or "n." I can't put



    !time.charAt(0) == 'y'


    As the condition because the string "time" is defined inside the do-while loop, so I made some weird band-aid by using one scanner just for the condition input before the do-while begins, and using another one inside the body. I know this is bad, but I can't think of a simple way to create a boolean for this condition that isn't inside the do-while loop, am I missing something?










    share|improve this question

























      1












      1








      1








      So I'm a little confused as to why this is happening, here's my code:



      public static void main (String args)
      {
      Scanner kb = new Scanner(System.in);

      do
      {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter a time in 24-hour notation: ");
      String time = in.nextLine();

      int colonIndex = time.indexOf(":");
      int hours = Integer.parseInt(time.substring(0, colonIndex));
      int minutes = Integer.parseInt(time.substring(colonIndex + 1));

      boolean legalTime = ((hours < 24 && hours > 0) && (minutes < 60 && minutes >= 0));
      boolean addZero = minutes < 10;
      boolean pm = hours > 12;

      if(legalTime)
      {
      if(pm && addZero)
      {
      int newHour = hours - 12;
      System.out.printf("That is the same as"
      + "n%d:0%d PMn", newHour, minutes);
      }
      else if(pm && !addZero)
      {
      hours = hours - 12;
      System.out.printf("That is the same as"
      + "n%d:%d PMn", hours, minutes);
      }
      else if (!pm && addZero)
      {
      System.out.printf("That is the same as"
      + "n%d:0%d AMn", hours, minutes);
      }
      else
      {
      System.out.printf("That is the same as"
      + "n%d:%d AMn", hours, minutes);
      }
      }
      try
      {
      if(!legalTime)
      {
      throw new Exception("Exception: there is no such time as " + time);
      }
      }
      catch(Exception e)
      {
      System.out.println(e.getMessage()
      + "nAgain? (y/n)");
      continue;
      }

      System.out.println("Again? (y/n)");

      }while(Character.toUpperCase(kb.next().charAt(0)) == 'Y');

      }


      My code itself is not the issue, it's that the condition of a do-while loop only recognizes booleans outside of the do-while, which in my mind makes it quite frustrating to make the condition be affected by whatever is inside the block. What I want to do is make my code run, and then ask the user if they want to run it again, denoted by "y" or "n." I can't put



      !time.charAt(0) == 'y'


      As the condition because the string "time" is defined inside the do-while loop, so I made some weird band-aid by using one scanner just for the condition input before the do-while begins, and using another one inside the body. I know this is bad, but I can't think of a simple way to create a boolean for this condition that isn't inside the do-while loop, am I missing something?










      share|improve this question














      So I'm a little confused as to why this is happening, here's my code:



      public static void main (String args)
      {
      Scanner kb = new Scanner(System.in);

      do
      {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter a time in 24-hour notation: ");
      String time = in.nextLine();

      int colonIndex = time.indexOf(":");
      int hours = Integer.parseInt(time.substring(0, colonIndex));
      int minutes = Integer.parseInt(time.substring(colonIndex + 1));

      boolean legalTime = ((hours < 24 && hours > 0) && (minutes < 60 && minutes >= 0));
      boolean addZero = minutes < 10;
      boolean pm = hours > 12;

      if(legalTime)
      {
      if(pm && addZero)
      {
      int newHour = hours - 12;
      System.out.printf("That is the same as"
      + "n%d:0%d PMn", newHour, minutes);
      }
      else if(pm && !addZero)
      {
      hours = hours - 12;
      System.out.printf("That is the same as"
      + "n%d:%d PMn", hours, minutes);
      }
      else if (!pm && addZero)
      {
      System.out.printf("That is the same as"
      + "n%d:0%d AMn", hours, minutes);
      }
      else
      {
      System.out.printf("That is the same as"
      + "n%d:%d AMn", hours, minutes);
      }
      }
      try
      {
      if(!legalTime)
      {
      throw new Exception("Exception: there is no such time as " + time);
      }
      }
      catch(Exception e)
      {
      System.out.println(e.getMessage()
      + "nAgain? (y/n)");
      continue;
      }

      System.out.println("Again? (y/n)");

      }while(Character.toUpperCase(kb.next().charAt(0)) == 'Y');

      }


      My code itself is not the issue, it's that the condition of a do-while loop only recognizes booleans outside of the do-while, which in my mind makes it quite frustrating to make the condition be affected by whatever is inside the block. What I want to do is make my code run, and then ask the user if they want to run it again, denoted by "y" or "n." I can't put



      !time.charAt(0) == 'y'


      As the condition because the string "time" is defined inside the do-while loop, so I made some weird band-aid by using one scanner just for the condition input before the do-while begins, and using another one inside the body. I know this is bad, but I can't think of a simple way to create a boolean for this condition that isn't inside the do-while loop, am I missing something?







      java boolean java.util.scanner condition do-while






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 7:48









      JSmithJSmith

      112




      112
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Just declare a boolean before the loop instead of the extra Scanner, and update this boolean inside the loop:



          boolean again = true;
          do {
          ...
          System.out.println("Again? (y/n)");
          again = Character.toUpperCase(in.next().charAt(0)) == 'Y';
          } while (again);





          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',
            autoActivateHeartbeat: false,
            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%2f53442568%2fduplicate-scanner-band-aid-for-do-while-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









            1














            Just declare a boolean before the loop instead of the extra Scanner, and update this boolean inside the loop:



            boolean again = true;
            do {
            ...
            System.out.println("Again? (y/n)");
            again = Character.toUpperCase(in.next().charAt(0)) == 'Y';
            } while (again);





            share|improve this answer




























              1














              Just declare a boolean before the loop instead of the extra Scanner, and update this boolean inside the loop:



              boolean again = true;
              do {
              ...
              System.out.println("Again? (y/n)");
              again = Character.toUpperCase(in.next().charAt(0)) == 'Y';
              } while (again);





              share|improve this answer


























                1












                1








                1







                Just declare a boolean before the loop instead of the extra Scanner, and update this boolean inside the loop:



                boolean again = true;
                do {
                ...
                System.out.println("Again? (y/n)");
                again = Character.toUpperCase(in.next().charAt(0)) == 'Y';
                } while (again);





                share|improve this answer













                Just declare a boolean before the loop instead of the extra Scanner, and update this boolean inside the loop:



                boolean again = true;
                do {
                ...
                System.out.println("Again? (y/n)");
                again = Character.toUpperCase(in.next().charAt(0)) == 'Y';
                } while (again);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 7:53









                EranEran

                290k37479563




                290k37479563
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442568%2fduplicate-scanner-band-aid-for-do-while-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







                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings