Printing a number based on a user-provided number

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












Is there a better way to ask for input and give answers, but without so many lines? Each input will give a specific output according to a key that I use, but this many lines seems excessive. I'm a beginner to java and could use some ideas on how to optimize this coding.



 import java.util.Scanner; 
class Test
public static void main (String args)

System.out.print ("Enter the day of the month - Answer: ");
Scanner inputFigure = new Scanner (System.in);
int i = inputFigure.nextInt ();
if ( i==1)
System.out.println ("12");

else if (i==2)
System.out.println ("13");

else if (i==3)
System.out.println ("14");

else if (i==4)
System.out.println ("11");

else if (i==5)
System.out.println ("12");

else if (i==6)
System.out.println ("13");

else if (i==7)
System.out.println ("14");

else if (i==8)
System.out.println ("11");

else if (i==9)
System.out.println ("12");

else if (i==10)
System.out.println ("21");

else if (i==11)
System.out.println ("22");

else if (i==12)
System.out.println ("23");

else if (i==13)
System.out.println ("24");

else if (i==14)
System.out.println ("21");

else if (i==15)
System.out.println ("22");

else if (i==16)
System.out.println ("23");

else if (i==17)
System.out.println ("24");

else if (i==18)
System.out.println ("21");

else if (i==19)
System.out.println ("22");

else if (i==20)
System.out.println ("31");

else if (i==21)
System.out.println ("32");

else if (i==22)
System.out.println ("33");

else if (i==23)
System.out.println ("34");

else if (i==24)
System.out.println ("31");

else if (i==25)
System.out.println ("32");

else if (i==26)
System.out.println ("33");

else if (i==27)
System.out.println ("34");

else if (i==28)
System.out.println ("31");

else if (i==29)
System.out.println ("32");

else if (i==30)
System.out.println ("41");

else if (i==31)
System.out.println ("42");

else
System.out.println ("Invalid entry for the day. Try again.");









share|improve this question



























    up vote
    1
    down vote

    favorite












    Is there a better way to ask for input and give answers, but without so many lines? Each input will give a specific output according to a key that I use, but this many lines seems excessive. I'm a beginner to java and could use some ideas on how to optimize this coding.



     import java.util.Scanner; 
    class Test
    public static void main (String args)

    System.out.print ("Enter the day of the month - Answer: ");
    Scanner inputFigure = new Scanner (System.in);
    int i = inputFigure.nextInt ();
    if ( i==1)
    System.out.println ("12");

    else if (i==2)
    System.out.println ("13");

    else if (i==3)
    System.out.println ("14");

    else if (i==4)
    System.out.println ("11");

    else if (i==5)
    System.out.println ("12");

    else if (i==6)
    System.out.println ("13");

    else if (i==7)
    System.out.println ("14");

    else if (i==8)
    System.out.println ("11");

    else if (i==9)
    System.out.println ("12");

    else if (i==10)
    System.out.println ("21");

    else if (i==11)
    System.out.println ("22");

    else if (i==12)
    System.out.println ("23");

    else if (i==13)
    System.out.println ("24");

    else if (i==14)
    System.out.println ("21");

    else if (i==15)
    System.out.println ("22");

    else if (i==16)
    System.out.println ("23");

    else if (i==17)
    System.out.println ("24");

    else if (i==18)
    System.out.println ("21");

    else if (i==19)
    System.out.println ("22");

    else if (i==20)
    System.out.println ("31");

    else if (i==21)
    System.out.println ("32");

    else if (i==22)
    System.out.println ("33");

    else if (i==23)
    System.out.println ("34");

    else if (i==24)
    System.out.println ("31");

    else if (i==25)
    System.out.println ("32");

    else if (i==26)
    System.out.println ("33");

    else if (i==27)
    System.out.println ("34");

    else if (i==28)
    System.out.println ("31");

    else if (i==29)
    System.out.println ("32");

    else if (i==30)
    System.out.println ("41");

    else if (i==31)
    System.out.println ("42");

    else
    System.out.println ("Invalid entry for the day. Try again.");









    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Is there a better way to ask for input and give answers, but without so many lines? Each input will give a specific output according to a key that I use, but this many lines seems excessive. I'm a beginner to java and could use some ideas on how to optimize this coding.



       import java.util.Scanner; 
      class Test
      public static void main (String args)

      System.out.print ("Enter the day of the month - Answer: ");
      Scanner inputFigure = new Scanner (System.in);
      int i = inputFigure.nextInt ();
      if ( i==1)
      System.out.println ("12");

      else if (i==2)
      System.out.println ("13");

      else if (i==3)
      System.out.println ("14");

      else if (i==4)
      System.out.println ("11");

      else if (i==5)
      System.out.println ("12");

      else if (i==6)
      System.out.println ("13");

      else if (i==7)
      System.out.println ("14");

      else if (i==8)
      System.out.println ("11");

      else if (i==9)
      System.out.println ("12");

      else if (i==10)
      System.out.println ("21");

      else if (i==11)
      System.out.println ("22");

      else if (i==12)
      System.out.println ("23");

      else if (i==13)
      System.out.println ("24");

      else if (i==14)
      System.out.println ("21");

      else if (i==15)
      System.out.println ("22");

      else if (i==16)
      System.out.println ("23");

      else if (i==17)
      System.out.println ("24");

      else if (i==18)
      System.out.println ("21");

      else if (i==19)
      System.out.println ("22");

      else if (i==20)
      System.out.println ("31");

      else if (i==21)
      System.out.println ("32");

      else if (i==22)
      System.out.println ("33");

      else if (i==23)
      System.out.println ("34");

      else if (i==24)
      System.out.println ("31");

      else if (i==25)
      System.out.println ("32");

      else if (i==26)
      System.out.println ("33");

      else if (i==27)
      System.out.println ("34");

      else if (i==28)
      System.out.println ("31");

      else if (i==29)
      System.out.println ("32");

      else if (i==30)
      System.out.println ("41");

      else if (i==31)
      System.out.println ("42");

      else
      System.out.println ("Invalid entry for the day. Try again.");









      share|improve this question













      Is there a better way to ask for input and give answers, but without so many lines? Each input will give a specific output according to a key that I use, but this many lines seems excessive. I'm a beginner to java and could use some ideas on how to optimize this coding.



       import java.util.Scanner; 
      class Test
      public static void main (String args)

      System.out.print ("Enter the day of the month - Answer: ");
      Scanner inputFigure = new Scanner (System.in);
      int i = inputFigure.nextInt ();
      if ( i==1)
      System.out.println ("12");

      else if (i==2)
      System.out.println ("13");

      else if (i==3)
      System.out.println ("14");

      else if (i==4)
      System.out.println ("11");

      else if (i==5)
      System.out.println ("12");

      else if (i==6)
      System.out.println ("13");

      else if (i==7)
      System.out.println ("14");

      else if (i==8)
      System.out.println ("11");

      else if (i==9)
      System.out.println ("12");

      else if (i==10)
      System.out.println ("21");

      else if (i==11)
      System.out.println ("22");

      else if (i==12)
      System.out.println ("23");

      else if (i==13)
      System.out.println ("24");

      else if (i==14)
      System.out.println ("21");

      else if (i==15)
      System.out.println ("22");

      else if (i==16)
      System.out.println ("23");

      else if (i==17)
      System.out.println ("24");

      else if (i==18)
      System.out.println ("21");

      else if (i==19)
      System.out.println ("22");

      else if (i==20)
      System.out.println ("31");

      else if (i==21)
      System.out.println ("32");

      else if (i==22)
      System.out.println ("33");

      else if (i==23)
      System.out.println ("34");

      else if (i==24)
      System.out.println ("31");

      else if (i==25)
      System.out.println ("32");

      else if (i==26)
      System.out.println ("33");

      else if (i==27)
      System.out.println ("34");

      else if (i==28)
      System.out.println ("31");

      else if (i==29)
      System.out.println ("32");

      else if (i==30)
      System.out.println ("41");

      else if (i==31)
      System.out.println ("42");

      else
      System.out.println ("Invalid entry for the day. Try again.");











      share|improve this question












      share|improve this question




      share|improve this question








      edited Feb 26 at 20:28









      200_success

      123k14142399




      123k14142399









      asked Feb 26 at 19:56









      Barnabas Showl

      114




      114




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote













          The best way to shorten the code would be to write a formula for it. That formula should be packaged into a function:



          public static String outputForDay(int day) 
          if (!(1 <= day && day <= 31))
          throw new IllegalArgumentException("Invalid entry for the day.");

          int onesDigit = day % 10,
          tensDigit = day / 10;
          return Integer.toString(10 * (tensDigit + 1) + (onesDigit % 4) + 1);



          Then, you would call the function like this:



          public static void main(String args) 
          try (Scanner scanner = new Scanner(System.in))
          System.out.print("Enter the day of the month: ");
          System.out.println(outputForDay(scanner.nextInt()));
          catch (IllegalArgumentException e)
          System.out.println(e.getMessage());







          share|improve this answer




























            up vote
            0
            down vote













            You can use what is called a Map.
            So basically you will create your map with



            Map<KEY, VALUE> map = new HashMap<KEY, VAlUE>();


            Your case would be something like this:



            map<Integer, String> map = new HashMap<Integer, String>();


            Then put the values as so:



            map.put("World/Value", "What is would equal");


            in your case, it would be



            map.put(1, "12").put(2,"13").build();


            To do what you are trying to do you can create a for loop and



            System.out.println(map.get(VALUE));


            For example,



            System.out.println(map.get(1));


            and this would return "12"






            share|improve this answer























            • Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
              – Imus
              Feb 28 at 8:16











            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            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: "196"
            ;
            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: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188395%2fprinting-a-number-based-on-a-user-provided-number%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            The best way to shorten the code would be to write a formula for it. That formula should be packaged into a function:



            public static String outputForDay(int day) 
            if (!(1 <= day && day <= 31))
            throw new IllegalArgumentException("Invalid entry for the day.");

            int onesDigit = day % 10,
            tensDigit = day / 10;
            return Integer.toString(10 * (tensDigit + 1) + (onesDigit % 4) + 1);



            Then, you would call the function like this:



            public static void main(String args) 
            try (Scanner scanner = new Scanner(System.in))
            System.out.print("Enter the day of the month: ");
            System.out.println(outputForDay(scanner.nextInt()));
            catch (IllegalArgumentException e)
            System.out.println(e.getMessage());







            share|improve this answer

























              up vote
              3
              down vote













              The best way to shorten the code would be to write a formula for it. That formula should be packaged into a function:



              public static String outputForDay(int day) 
              if (!(1 <= day && day <= 31))
              throw new IllegalArgumentException("Invalid entry for the day.");

              int onesDigit = day % 10,
              tensDigit = day / 10;
              return Integer.toString(10 * (tensDigit + 1) + (onesDigit % 4) + 1);



              Then, you would call the function like this:



              public static void main(String args) 
              try (Scanner scanner = new Scanner(System.in))
              System.out.print("Enter the day of the month: ");
              System.out.println(outputForDay(scanner.nextInt()));
              catch (IllegalArgumentException e)
              System.out.println(e.getMessage());







              share|improve this answer























                up vote
                3
                down vote










                up vote
                3
                down vote









                The best way to shorten the code would be to write a formula for it. That formula should be packaged into a function:



                public static String outputForDay(int day) 
                if (!(1 <= day && day <= 31))
                throw new IllegalArgumentException("Invalid entry for the day.");

                int onesDigit = day % 10,
                tensDigit = day / 10;
                return Integer.toString(10 * (tensDigit + 1) + (onesDigit % 4) + 1);



                Then, you would call the function like this:



                public static void main(String args) 
                try (Scanner scanner = new Scanner(System.in))
                System.out.print("Enter the day of the month: ");
                System.out.println(outputForDay(scanner.nextInt()));
                catch (IllegalArgumentException e)
                System.out.println(e.getMessage());







                share|improve this answer













                The best way to shorten the code would be to write a formula for it. That formula should be packaged into a function:



                public static String outputForDay(int day) 
                if (!(1 <= day && day <= 31))
                throw new IllegalArgumentException("Invalid entry for the day.");

                int onesDigit = day % 10,
                tensDigit = day / 10;
                return Integer.toString(10 * (tensDigit + 1) + (onesDigit % 4) + 1);



                Then, you would call the function like this:



                public static void main(String args) 
                try (Scanner scanner = new Scanner(System.in))
                System.out.print("Enter the day of the month: ");
                System.out.println(outputForDay(scanner.nextInt()));
                catch (IllegalArgumentException e)
                System.out.println(e.getMessage());








                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Feb 26 at 20:44









                200_success

                123k14142399




                123k14142399






















                    up vote
                    0
                    down vote













                    You can use what is called a Map.
                    So basically you will create your map with



                    Map<KEY, VALUE> map = new HashMap<KEY, VAlUE>();


                    Your case would be something like this:



                    map<Integer, String> map = new HashMap<Integer, String>();


                    Then put the values as so:



                    map.put("World/Value", "What is would equal");


                    in your case, it would be



                    map.put(1, "12").put(2,"13").build();


                    To do what you are trying to do you can create a for loop and



                    System.out.println(map.get(VALUE));


                    For example,



                    System.out.println(map.get(1));


                    and this would return "12"






                    share|improve this answer























                    • Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
                      – Imus
                      Feb 28 at 8:16















                    up vote
                    0
                    down vote













                    You can use what is called a Map.
                    So basically you will create your map with



                    Map<KEY, VALUE> map = new HashMap<KEY, VAlUE>();


                    Your case would be something like this:



                    map<Integer, String> map = new HashMap<Integer, String>();


                    Then put the values as so:



                    map.put("World/Value", "What is would equal");


                    in your case, it would be



                    map.put(1, "12").put(2,"13").build();


                    To do what you are trying to do you can create a for loop and



                    System.out.println(map.get(VALUE));


                    For example,



                    System.out.println(map.get(1));


                    and this would return "12"






                    share|improve this answer























                    • Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
                      – Imus
                      Feb 28 at 8:16













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    You can use what is called a Map.
                    So basically you will create your map with



                    Map<KEY, VALUE> map = new HashMap<KEY, VAlUE>();


                    Your case would be something like this:



                    map<Integer, String> map = new HashMap<Integer, String>();


                    Then put the values as so:



                    map.put("World/Value", "What is would equal");


                    in your case, it would be



                    map.put(1, "12").put(2,"13").build();


                    To do what you are trying to do you can create a for loop and



                    System.out.println(map.get(VALUE));


                    For example,



                    System.out.println(map.get(1));


                    and this would return "12"






                    share|improve this answer















                    You can use what is called a Map.
                    So basically you will create your map with



                    Map<KEY, VALUE> map = new HashMap<KEY, VAlUE>();


                    Your case would be something like this:



                    map<Integer, String> map = new HashMap<Integer, String>();


                    Then put the values as so:



                    map.put("World/Value", "What is would equal");


                    in your case, it would be



                    map.put(1, "12").put(2,"13").build();


                    To do what you are trying to do you can create a for loop and



                    System.out.println(map.get(VALUE));


                    For example,



                    System.out.println(map.get(1));


                    and this would return "12"







                    share|improve this answer















                    share|improve this answer



                    share|improve this answer








                    edited Feb 26 at 22:01


























                    answered Feb 26 at 20:47









                    RithvikK

                    114




                    114











                    • Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
                      – Imus
                      Feb 28 at 8:16

















                    • Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
                      – Imus
                      Feb 28 at 8:16
















                    Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
                    – Imus
                    Feb 28 at 8:16





                    Although true, you just shift the problem from having too many lines in if checks, to too many lines to populate a map. If 200_success' idea of a formula isn't possible and you also include a way to populate the map from say a CSV file you got a nice alternative solution.
                    – Imus
                    Feb 28 at 8:16













                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188395%2fprinting-a-number-based-on-a-user-provided-number%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation