The first position of my array isn't being modified










0















I'm creating an application that converts a 5 digit zip code into frame bars. Weirdly, the first digit entered doesn't correctly go through the conversion. Here's the code:



import java.util.*;
import java.lang.*;
import java.io.*;
class Zip

public static String checkDigit(String bar)

Scanner s = new Scanner(System.in);
int input;
input = new int [4];
for(int p = 0; p < 4; p++)

input[p] = s.nextInt();
if (input[p] == 0)
:::";

if (input[p] == 1)


System.out.println();
return bar;


public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));




For example, when entering 1, 0, 0, 0, 0 into the array, the output is



"1||:::||:::||:::||:::"



When entering 0, 1, 1, 1, 1 into the array, the output is



"0:::||:::||:::||:::||"



Instead of converting the first digit at pos [0], it just prints the number outright. Why is this?










share|improve this question

















  • 1





    Because the initial value of bar is retained when you use bar = bar + ...

    – Jacob G.
    Nov 14 '18 at 21:00
















0















I'm creating an application that converts a 5 digit zip code into frame bars. Weirdly, the first digit entered doesn't correctly go through the conversion. Here's the code:



import java.util.*;
import java.lang.*;
import java.io.*;
class Zip

public static String checkDigit(String bar)

Scanner s = new Scanner(System.in);
int input;
input = new int [4];
for(int p = 0; p < 4; p++)

input[p] = s.nextInt();
if (input[p] == 0)
:::";

if (input[p] == 1)


System.out.println();
return bar;


public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));




For example, when entering 1, 0, 0, 0, 0 into the array, the output is



"1||:::||:::||:::||:::"



When entering 0, 1, 1, 1, 1 into the array, the output is



"0:::||:::||:::||:::||"



Instead of converting the first digit at pos [0], it just prints the number outright. Why is this?










share|improve this question

















  • 1





    Because the initial value of bar is retained when you use bar = bar + ...

    – Jacob G.
    Nov 14 '18 at 21:00














0












0








0








I'm creating an application that converts a 5 digit zip code into frame bars. Weirdly, the first digit entered doesn't correctly go through the conversion. Here's the code:



import java.util.*;
import java.lang.*;
import java.io.*;
class Zip

public static String checkDigit(String bar)

Scanner s = new Scanner(System.in);
int input;
input = new int [4];
for(int p = 0; p < 4; p++)

input[p] = s.nextInt();
if (input[p] == 0)
:::";

if (input[p] == 1)


System.out.println();
return bar;


public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));




For example, when entering 1, 0, 0, 0, 0 into the array, the output is



"1||:::||:::||:::||:::"



When entering 0, 1, 1, 1, 1 into the array, the output is



"0:::||:::||:::||:::||"



Instead of converting the first digit at pos [0], it just prints the number outright. Why is this?










share|improve this question














I'm creating an application that converts a 5 digit zip code into frame bars. Weirdly, the first digit entered doesn't correctly go through the conversion. Here's the code:



import java.util.*;
import java.lang.*;
import java.io.*;
class Zip

public static String checkDigit(String bar)

Scanner s = new Scanner(System.in);
int input;
input = new int [4];
for(int p = 0; p < 4; p++)

input[p] = s.nextInt();
if (input[p] == 0)
:::";

if (input[p] == 1)


System.out.println();
return bar;


public static void main (String args)
Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));




For example, when entering 1, 0, 0, 0, 0 into the array, the output is



"1||:::||:::||:::||:::"



When entering 0, 1, 1, 1, 1 into the array, the output is



"0:::||:::||:::||:::||"



Instead of converting the first digit at pos [0], it just prints the number outright. Why is this?







java arrays for-loop zipcode






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 20:56









ddalcantoddalcanto

254




254







  • 1





    Because the initial value of bar is retained when you use bar = bar + ...

    – Jacob G.
    Nov 14 '18 at 21:00













  • 1





    Because the initial value of bar is retained when you use bar = bar + ...

    – Jacob G.
    Nov 14 '18 at 21:00








1




1





Because the initial value of bar is retained when you use bar = bar + ...

– Jacob G.
Nov 14 '18 at 21:00






Because the initial value of bar is retained when you use bar = bar + ...

– Jacob G.
Nov 14 '18 at 21:00













3 Answers
3






active

oldest

votes


















1














It is because of s.nextLine() it will read the first int entered in console and save it in String bar passing into checkDigit()



Scanner s = new Scanner(System.in);
System.out.println(checkDigit(s.nextLine()));


Pass an empty string



Scanner s = new Scanner(System.in);
System.out.println(checkDigit(""));


Input : 0 1 1 1



Output : ||::::::||:::||:::||






share|improve this answer






























    1














    There's not much of a problem here actually @ddalcanto. Your logic is pretty much alright.. Just a few minor mistakes which are causing the anomalies in the output.



    1. The 1st input is displayed as it is because you are appending the updated values to the bar string which is nothing but the first accepted character because of the s.nextLine() in the main() function. Hence, see point number 2.

    2. There's no need to send the s.nextLine() as argument to the function call of checkDigit(). Also remove the string bar as parameter from the function definition too. This is what is causing you to give an extra input. For example, 5 inputs for intended 4 inputs, etc.

    3. Your input array is of size 4, but you are looping from 0 to 4, which means 5 times to get 5 inputs, but the array can hold just 4 elemnts. Just change the array size to 5. => input = new int[5];

    Here is the updated working code along with the associated output.



    CODE:



    import java.util.*;
    import java.lang.*;
    import java.io.*;
    class Zip

    public static String checkDigit()

    Scanner s = new Scanner(System.in);
    String bar = "";
    int input;
    input = new int [5];
    for(int p = 0; p < 5; p++)

    input[p] = s.nextInt();
    if (input[p] == 0)
    :::";

    if (input[p] == 1)


    System.out.println();
    return bar;


    public static void main (String args)
    Scanner s = new Scanner(System.in);
    System.out.println(checkDigit());




    OUTPUT:




     1
    1
    1
    1
    1

    :::||:::||:::||:::||:::||



    Another input-output pair:




     0
    1
    1
    0
    1

    ||::::::||:::||||::::::||






    share|improve this answer






























      0














      You are creating and reading from a Scanner object in main() so the first digit goes into the bar parameter sent to the checkDigit method.



      I would remove the bar parameter altogether and also make some other changes.



      public static String checkDigit() 
      StringBuilder bar = new StringBuilder();
      Scanner s = new Scanner(System.in);
      int input = 0;
      for(int p = 0; p < 5; p++)
      input = s.nextInt();
      if (input == 0)
      if (input == 1) ");


      return bar.toString();






      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%2f53308591%2fthe-first-position-of-my-array-isnt-being-modified%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        It is because of s.nextLine() it will read the first int entered in console and save it in String bar passing into checkDigit()



        Scanner s = new Scanner(System.in);
        System.out.println(checkDigit(s.nextLine()));


        Pass an empty string



        Scanner s = new Scanner(System.in);
        System.out.println(checkDigit(""));


        Input : 0 1 1 1



        Output : ||::::::||:::||:::||






        share|improve this answer



























          1














          It is because of s.nextLine() it will read the first int entered in console and save it in String bar passing into checkDigit()



          Scanner s = new Scanner(System.in);
          System.out.println(checkDigit(s.nextLine()));


          Pass an empty string



          Scanner s = new Scanner(System.in);
          System.out.println(checkDigit(""));


          Input : 0 1 1 1



          Output : ||::::::||:::||:::||






          share|improve this answer

























            1












            1








            1







            It is because of s.nextLine() it will read the first int entered in console and save it in String bar passing into checkDigit()



            Scanner s = new Scanner(System.in);
            System.out.println(checkDigit(s.nextLine()));


            Pass an empty string



            Scanner s = new Scanner(System.in);
            System.out.println(checkDigit(""));


            Input : 0 1 1 1



            Output : ||::::::||:::||:::||






            share|improve this answer













            It is because of s.nextLine() it will read the first int entered in console and save it in String bar passing into checkDigit()



            Scanner s = new Scanner(System.in);
            System.out.println(checkDigit(s.nextLine()));


            Pass an empty string



            Scanner s = new Scanner(System.in);
            System.out.println(checkDigit(""));


            Input : 0 1 1 1



            Output : ||::::::||:::||:::||







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 21:02









            DeadpoolDeadpool

            7,3572829




            7,3572829























                1














                There's not much of a problem here actually @ddalcanto. Your logic is pretty much alright.. Just a few minor mistakes which are causing the anomalies in the output.



                1. The 1st input is displayed as it is because you are appending the updated values to the bar string which is nothing but the first accepted character because of the s.nextLine() in the main() function. Hence, see point number 2.

                2. There's no need to send the s.nextLine() as argument to the function call of checkDigit(). Also remove the string bar as parameter from the function definition too. This is what is causing you to give an extra input. For example, 5 inputs for intended 4 inputs, etc.

                3. Your input array is of size 4, but you are looping from 0 to 4, which means 5 times to get 5 inputs, but the array can hold just 4 elemnts. Just change the array size to 5. => input = new int[5];

                Here is the updated working code along with the associated output.



                CODE:



                import java.util.*;
                import java.lang.*;
                import java.io.*;
                class Zip

                public static String checkDigit()

                Scanner s = new Scanner(System.in);
                String bar = "";
                int input;
                input = new int [5];
                for(int p = 0; p < 5; p++)

                input[p] = s.nextInt();
                if (input[p] == 0)
                :::";

                if (input[p] == 1)


                System.out.println();
                return bar;


                public static void main (String args)
                Scanner s = new Scanner(System.in);
                System.out.println(checkDigit());




                OUTPUT:




                 1
                1
                1
                1
                1

                :::||:::||:::||:::||:::||



                Another input-output pair:




                 0
                1
                1
                0
                1

                ||::::::||:::||||::::::||






                share|improve this answer



























                  1














                  There's not much of a problem here actually @ddalcanto. Your logic is pretty much alright.. Just a few minor mistakes which are causing the anomalies in the output.



                  1. The 1st input is displayed as it is because you are appending the updated values to the bar string which is nothing but the first accepted character because of the s.nextLine() in the main() function. Hence, see point number 2.

                  2. There's no need to send the s.nextLine() as argument to the function call of checkDigit(). Also remove the string bar as parameter from the function definition too. This is what is causing you to give an extra input. For example, 5 inputs for intended 4 inputs, etc.

                  3. Your input array is of size 4, but you are looping from 0 to 4, which means 5 times to get 5 inputs, but the array can hold just 4 elemnts. Just change the array size to 5. => input = new int[5];

                  Here is the updated working code along with the associated output.



                  CODE:



                  import java.util.*;
                  import java.lang.*;
                  import java.io.*;
                  class Zip

                  public static String checkDigit()

                  Scanner s = new Scanner(System.in);
                  String bar = "";
                  int input;
                  input = new int [5];
                  for(int p = 0; p < 5; p++)

                  input[p] = s.nextInt();
                  if (input[p] == 0)
                  :::";

                  if (input[p] == 1)


                  System.out.println();
                  return bar;


                  public static void main (String args)
                  Scanner s = new Scanner(System.in);
                  System.out.println(checkDigit());




                  OUTPUT:




                   1
                  1
                  1
                  1
                  1

                  :::||:::||:::||:::||:::||



                  Another input-output pair:




                   0
                  1
                  1
                  0
                  1

                  ||::::::||:::||||::::::||






                  share|improve this answer

























                    1












                    1








                    1







                    There's not much of a problem here actually @ddalcanto. Your logic is pretty much alright.. Just a few minor mistakes which are causing the anomalies in the output.



                    1. The 1st input is displayed as it is because you are appending the updated values to the bar string which is nothing but the first accepted character because of the s.nextLine() in the main() function. Hence, see point number 2.

                    2. There's no need to send the s.nextLine() as argument to the function call of checkDigit(). Also remove the string bar as parameter from the function definition too. This is what is causing you to give an extra input. For example, 5 inputs for intended 4 inputs, etc.

                    3. Your input array is of size 4, but you are looping from 0 to 4, which means 5 times to get 5 inputs, but the array can hold just 4 elemnts. Just change the array size to 5. => input = new int[5];

                    Here is the updated working code along with the associated output.



                    CODE:



                    import java.util.*;
                    import java.lang.*;
                    import java.io.*;
                    class Zip

                    public static String checkDigit()

                    Scanner s = new Scanner(System.in);
                    String bar = "";
                    int input;
                    input = new int [5];
                    for(int p = 0; p < 5; p++)

                    input[p] = s.nextInt();
                    if (input[p] == 0)
                    :::";

                    if (input[p] == 1)


                    System.out.println();
                    return bar;


                    public static void main (String args)
                    Scanner s = new Scanner(System.in);
                    System.out.println(checkDigit());




                    OUTPUT:




                     1
                    1
                    1
                    1
                    1

                    :::||:::||:::||:::||:::||



                    Another input-output pair:




                     0
                    1
                    1
                    0
                    1

                    ||::::::||:::||||::::::||






                    share|improve this answer













                    There's not much of a problem here actually @ddalcanto. Your logic is pretty much alright.. Just a few minor mistakes which are causing the anomalies in the output.



                    1. The 1st input is displayed as it is because you are appending the updated values to the bar string which is nothing but the first accepted character because of the s.nextLine() in the main() function. Hence, see point number 2.

                    2. There's no need to send the s.nextLine() as argument to the function call of checkDigit(). Also remove the string bar as parameter from the function definition too. This is what is causing you to give an extra input. For example, 5 inputs for intended 4 inputs, etc.

                    3. Your input array is of size 4, but you are looping from 0 to 4, which means 5 times to get 5 inputs, but the array can hold just 4 elemnts. Just change the array size to 5. => input = new int[5];

                    Here is the updated working code along with the associated output.



                    CODE:



                    import java.util.*;
                    import java.lang.*;
                    import java.io.*;
                    class Zip

                    public static String checkDigit()

                    Scanner s = new Scanner(System.in);
                    String bar = "";
                    int input;
                    input = new int [5];
                    for(int p = 0; p < 5; p++)

                    input[p] = s.nextInt();
                    if (input[p] == 0)
                    :::";

                    if (input[p] == 1)


                    System.out.println();
                    return bar;


                    public static void main (String args)
                    Scanner s = new Scanner(System.in);
                    System.out.println(checkDigit());




                    OUTPUT:




                     1
                    1
                    1
                    1
                    1

                    :::||:::||:::||:::||:::||



                    Another input-output pair:




                     0
                    1
                    1
                    0
                    1

                    ||::::::||:::||||::::::||







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 5:21









                    RaiRai

                    1,0263822




                    1,0263822





















                        0














                        You are creating and reading from a Scanner object in main() so the first digit goes into the bar parameter sent to the checkDigit method.



                        I would remove the bar parameter altogether and also make some other changes.



                        public static String checkDigit() 
                        StringBuilder bar = new StringBuilder();
                        Scanner s = new Scanner(System.in);
                        int input = 0;
                        for(int p = 0; p < 5; p++)
                        input = s.nextInt();
                        if (input == 0)
                        if (input == 1) ");


                        return bar.toString();






                        share|improve this answer





























                          0














                          You are creating and reading from a Scanner object in main() so the first digit goes into the bar parameter sent to the checkDigit method.



                          I would remove the bar parameter altogether and also make some other changes.



                          public static String checkDigit() 
                          StringBuilder bar = new StringBuilder();
                          Scanner s = new Scanner(System.in);
                          int input = 0;
                          for(int p = 0; p < 5; p++)
                          input = s.nextInt();
                          if (input == 0)
                          if (input == 1) ");


                          return bar.toString();






                          share|improve this answer



























                            0












                            0








                            0







                            You are creating and reading from a Scanner object in main() so the first digit goes into the bar parameter sent to the checkDigit method.



                            I would remove the bar parameter altogether and also make some other changes.



                            public static String checkDigit() 
                            StringBuilder bar = new StringBuilder();
                            Scanner s = new Scanner(System.in);
                            int input = 0;
                            for(int p = 0; p < 5; p++)
                            input = s.nextInt();
                            if (input == 0)
                            if (input == 1) ");


                            return bar.toString();






                            share|improve this answer















                            You are creating and reading from a Scanner object in main() so the first digit goes into the bar parameter sent to the checkDigit method.



                            I would remove the bar parameter altogether and also make some other changes.



                            public static String checkDigit() 
                            StringBuilder bar = new StringBuilder();
                            Scanner s = new Scanner(System.in);
                            int input = 0;
                            for(int p = 0; p < 5; p++)
                            input = s.nextInt();
                            if (input == 0)
                            if (input == 1) ");


                            return bar.toString();







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 14 '18 at 21:06

























                            answered Nov 14 '18 at 21:01









                            Joakim DanielsonJoakim Danielson

                            10.2k3725




                            10.2k3725



























                                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%2f53308591%2fthe-first-position-of-my-array-isnt-being-modified%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

                                Use pre created SQLite database for Android project in kotlin

                                Darth Vader #20

                                Ondo