summing 2 arrays to third array












0















i'm trying to make a program for my college task. it's about summing 2 arrays and the output into third array. but there's an error ArrayOutOfBoundsException but idk where's the error.

here's my source code



Scanner sc = new Scanner(System.in);
int arr1 = new int[3];
int arr2 = new int[3];
int i, j, k;
for(i = 0; i < 3; i++) {
System.out.print("Masukkan array pertama ke-" + i + ": ");
arr1[i] = sc.nextInt();
}

for(j = 0; j < 3; j++) {
System.out.print("Masukkan array kedua ke-" + j + ": ");
arr2[j] = sc.nextInt();
}

for(k = 0; k < 4; k++) {
int arr3 = new int[3];
System.out.println(arr3[k] = arr1[i] + arr2[j]);
}


the expected is like this



arr1 = {1, 2, 3, 4}  
arr2 = {1, 2, 3, 4}
arr3 = {2, 4, 6, 8}









share|improve this question

























  • arr3 is created on each loop iteration (which doesn't make sense) and surrounding for-loop has wrong conditional expression.

    – Michael Butscher
    Nov 13 '18 at 12:12











  • for(k = 0; k < 4; k++) is probably your issue. Shouldn't that be k < 3?

    – OldCurmudgeon
    Nov 13 '18 at 12:21
















0















i'm trying to make a program for my college task. it's about summing 2 arrays and the output into third array. but there's an error ArrayOutOfBoundsException but idk where's the error.

here's my source code



Scanner sc = new Scanner(System.in);
int arr1 = new int[3];
int arr2 = new int[3];
int i, j, k;
for(i = 0; i < 3; i++) {
System.out.print("Masukkan array pertama ke-" + i + ": ");
arr1[i] = sc.nextInt();
}

for(j = 0; j < 3; j++) {
System.out.print("Masukkan array kedua ke-" + j + ": ");
arr2[j] = sc.nextInt();
}

for(k = 0; k < 4; k++) {
int arr3 = new int[3];
System.out.println(arr3[k] = arr1[i] + arr2[j]);
}


the expected is like this



arr1 = {1, 2, 3, 4}  
arr2 = {1, 2, 3, 4}
arr3 = {2, 4, 6, 8}









share|improve this question

























  • arr3 is created on each loop iteration (which doesn't make sense) and surrounding for-loop has wrong conditional expression.

    – Michael Butscher
    Nov 13 '18 at 12:12











  • for(k = 0; k < 4; k++) is probably your issue. Shouldn't that be k < 3?

    – OldCurmudgeon
    Nov 13 '18 at 12:21














0












0








0








i'm trying to make a program for my college task. it's about summing 2 arrays and the output into third array. but there's an error ArrayOutOfBoundsException but idk where's the error.

here's my source code



Scanner sc = new Scanner(System.in);
int arr1 = new int[3];
int arr2 = new int[3];
int i, j, k;
for(i = 0; i < 3; i++) {
System.out.print("Masukkan array pertama ke-" + i + ": ");
arr1[i] = sc.nextInt();
}

for(j = 0; j < 3; j++) {
System.out.print("Masukkan array kedua ke-" + j + ": ");
arr2[j] = sc.nextInt();
}

for(k = 0; k < 4; k++) {
int arr3 = new int[3];
System.out.println(arr3[k] = arr1[i] + arr2[j]);
}


the expected is like this



arr1 = {1, 2, 3, 4}  
arr2 = {1, 2, 3, 4}
arr3 = {2, 4, 6, 8}









share|improve this question
















i'm trying to make a program for my college task. it's about summing 2 arrays and the output into third array. but there's an error ArrayOutOfBoundsException but idk where's the error.

here's my source code



Scanner sc = new Scanner(System.in);
int arr1 = new int[3];
int arr2 = new int[3];
int i, j, k;
for(i = 0; i < 3; i++) {
System.out.print("Masukkan array pertama ke-" + i + ": ");
arr1[i] = sc.nextInt();
}

for(j = 0; j < 3; j++) {
System.out.print("Masukkan array kedua ke-" + j + ": ");
arr2[j] = sc.nextInt();
}

for(k = 0; k < 4; k++) {
int arr3 = new int[3];
System.out.println(arr3[k] = arr1[i] + arr2[j]);
}


the expected is like this



arr1 = {1, 2, 3, 4}  
arr2 = {1, 2, 3, 4}
arr3 = {2, 4, 6, 8}






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 12:29









Suraj Rao

23k85770




23k85770










asked Nov 13 '18 at 12:10









Rizky Putra Pradhana BudimanRizky Putra Pradhana Budiman

11




11













  • arr3 is created on each loop iteration (which doesn't make sense) and surrounding for-loop has wrong conditional expression.

    – Michael Butscher
    Nov 13 '18 at 12:12











  • for(k = 0; k < 4; k++) is probably your issue. Shouldn't that be k < 3?

    – OldCurmudgeon
    Nov 13 '18 at 12:21



















  • arr3 is created on each loop iteration (which doesn't make sense) and surrounding for-loop has wrong conditional expression.

    – Michael Butscher
    Nov 13 '18 at 12:12











  • for(k = 0; k < 4; k++) is probably your issue. Shouldn't that be k < 3?

    – OldCurmudgeon
    Nov 13 '18 at 12:21

















arr3 is created on each loop iteration (which doesn't make sense) and surrounding for-loop has wrong conditional expression.

– Michael Butscher
Nov 13 '18 at 12:12





arr3 is created on each loop iteration (which doesn't make sense) and surrounding for-loop has wrong conditional expression.

– Michael Butscher
Nov 13 '18 at 12:12













for(k = 0; k < 4; k++) is probably your issue. Shouldn't that be k < 3?

– OldCurmudgeon
Nov 13 '18 at 12:21





for(k = 0; k < 4; k++) is probably your issue. Shouldn't that be k < 3?

– OldCurmudgeon
Nov 13 '18 at 12:21












3 Answers
3






active

oldest

votes


















0














for(k = 0; k < 4; k++) {
int arr3 = new int[3];
System.out.println(arr3[k] = arr1[i] + arr2[j]);
}


two major mistakes here. You do k < 4, while you should do k < 3
Also, you shouldn't re-create the array each iteration. Try this:



int arr3 = new int[3];
for(k = 0; k < 3; k++) {
arr3[k] = arr1[k] + arr2[k]; // i and j don't make sense here.
}


Printing the arrays, you can do after this.






share|improve this answer


























  • thank you, silly me for doing such a simple mistake!

    – Rizky Putra Pradhana Budiman
    Nov 13 '18 at 12:31



















0














Based on your expected output, your array size should be 4 instead of 3. Your code should look like this



    int arr1 = new int[4]; 
int arr2 = new int[4];


Your code received an ArrayOutOfBoundsException because the k in your 3rd for loop should be 3 instead of 4 since your k should be less than your array size which is 3. One thing to mention is that instead of hard-coding your array size, it is a better practice to assign a variable to store the size of the array so that you will not need to keep track of the size. Or you can just do this every time you use a for loop:



    k < array.length





share|improve this answer































    -1














    The problem is the input length is 3 and the output length is 4.



    Scanner sc = new Scanner(System.in);
    int arr1 = new int[3];
    int arr2 = new int[3];
    int i, j, k;

    //Changed 3 below to 4

    for(i = 0; i < 4; i++) {
    System.out.print("Masukkan array pertama ke-" + i + ": ");
    arr1[i] = sc.nextInt();
    }

    //Changed 3 below to 4;

    for(j = 0; j < 4; j++) {
    System.out.print("Masukkan array kedua ke-" + j + ": ");
    arr2[j] = sc.nextInt();
    }

    for(k = 0; k < 4; k++) {
    int arr3 = new int[3];
    System.out.println(arr3[k] = arr1[i] + arr2[j]);
    }





    share|improve this answer



















    • 1





      this answer is plain wrong. your code would crash on arrayIndexOutOfBounds. The length of the arrays is still 3, while you are going to look for fourth elements

      – Stultuske
      Nov 13 '18 at 12:23











    • Using this data: arr1 = {1, 2, 3, 4} arr2 = {1, 2, 3, 4} arr3 = {2, 4, 6, 8} the length of each array is 4.

      – Jolaosho batmat
      Nov 13 '18 at 12:35













    • int arr1 = new int[3]; int arr2 = new int[3];

      – Stultuske
      Nov 13 '18 at 14:20











    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%2f53280742%2fsumming-2-arrays-to-third-array%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









    0














    for(k = 0; k < 4; k++) {
    int arr3 = new int[3];
    System.out.println(arr3[k] = arr1[i] + arr2[j]);
    }


    two major mistakes here. You do k < 4, while you should do k < 3
    Also, you shouldn't re-create the array each iteration. Try this:



    int arr3 = new int[3];
    for(k = 0; k < 3; k++) {
    arr3[k] = arr1[k] + arr2[k]; // i and j don't make sense here.
    }


    Printing the arrays, you can do after this.






    share|improve this answer


























    • thank you, silly me for doing such a simple mistake!

      – Rizky Putra Pradhana Budiman
      Nov 13 '18 at 12:31
















    0














    for(k = 0; k < 4; k++) {
    int arr3 = new int[3];
    System.out.println(arr3[k] = arr1[i] + arr2[j]);
    }


    two major mistakes here. You do k < 4, while you should do k < 3
    Also, you shouldn't re-create the array each iteration. Try this:



    int arr3 = new int[3];
    for(k = 0; k < 3; k++) {
    arr3[k] = arr1[k] + arr2[k]; // i and j don't make sense here.
    }


    Printing the arrays, you can do after this.






    share|improve this answer


























    • thank you, silly me for doing such a simple mistake!

      – Rizky Putra Pradhana Budiman
      Nov 13 '18 at 12:31














    0












    0








    0







    for(k = 0; k < 4; k++) {
    int arr3 = new int[3];
    System.out.println(arr3[k] = arr1[i] + arr2[j]);
    }


    two major mistakes here. You do k < 4, while you should do k < 3
    Also, you shouldn't re-create the array each iteration. Try this:



    int arr3 = new int[3];
    for(k = 0; k < 3; k++) {
    arr3[k] = arr1[k] + arr2[k]; // i and j don't make sense here.
    }


    Printing the arrays, you can do after this.






    share|improve this answer















    for(k = 0; k < 4; k++) {
    int arr3 = new int[3];
    System.out.println(arr3[k] = arr1[i] + arr2[j]);
    }


    two major mistakes here. You do k < 4, while you should do k < 3
    Also, you shouldn't re-create the array each iteration. Try this:



    int arr3 = new int[3];
    for(k = 0; k < 3; k++) {
    arr3[k] = arr1[k] + arr2[k]; // i and j don't make sense here.
    }


    Printing the arrays, you can do after this.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 12:23

























    answered Nov 13 '18 at 12:12









    StultuskeStultuske

    6,21111626




    6,21111626













    • thank you, silly me for doing such a simple mistake!

      – Rizky Putra Pradhana Budiman
      Nov 13 '18 at 12:31



















    • thank you, silly me for doing such a simple mistake!

      – Rizky Putra Pradhana Budiman
      Nov 13 '18 at 12:31

















    thank you, silly me for doing such a simple mistake!

    – Rizky Putra Pradhana Budiman
    Nov 13 '18 at 12:31





    thank you, silly me for doing such a simple mistake!

    – Rizky Putra Pradhana Budiman
    Nov 13 '18 at 12:31













    0














    Based on your expected output, your array size should be 4 instead of 3. Your code should look like this



        int arr1 = new int[4]; 
    int arr2 = new int[4];


    Your code received an ArrayOutOfBoundsException because the k in your 3rd for loop should be 3 instead of 4 since your k should be less than your array size which is 3. One thing to mention is that instead of hard-coding your array size, it is a better practice to assign a variable to store the size of the array so that you will not need to keep track of the size. Or you can just do this every time you use a for loop:



        k < array.length





    share|improve this answer




























      0














      Based on your expected output, your array size should be 4 instead of 3. Your code should look like this



          int arr1 = new int[4]; 
      int arr2 = new int[4];


      Your code received an ArrayOutOfBoundsException because the k in your 3rd for loop should be 3 instead of 4 since your k should be less than your array size which is 3. One thing to mention is that instead of hard-coding your array size, it is a better practice to assign a variable to store the size of the array so that you will not need to keep track of the size. Or you can just do this every time you use a for loop:



          k < array.length





      share|improve this answer


























        0












        0








        0







        Based on your expected output, your array size should be 4 instead of 3. Your code should look like this



            int arr1 = new int[4]; 
        int arr2 = new int[4];


        Your code received an ArrayOutOfBoundsException because the k in your 3rd for loop should be 3 instead of 4 since your k should be less than your array size which is 3. One thing to mention is that instead of hard-coding your array size, it is a better practice to assign a variable to store the size of the array so that you will not need to keep track of the size. Or you can just do this every time you use a for loop:



            k < array.length





        share|improve this answer













        Based on your expected output, your array size should be 4 instead of 3. Your code should look like this



            int arr1 = new int[4]; 
        int arr2 = new int[4];


        Your code received an ArrayOutOfBoundsException because the k in your 3rd for loop should be 3 instead of 4 since your k should be less than your array size which is 3. One thing to mention is that instead of hard-coding your array size, it is a better practice to assign a variable to store the size of the array so that you will not need to keep track of the size. Or you can just do this every time you use a for loop:



            k < array.length






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 23:25









        siang swee kongsiang swee kong

        663




        663























            -1














            The problem is the input length is 3 and the output length is 4.



            Scanner sc = new Scanner(System.in);
            int arr1 = new int[3];
            int arr2 = new int[3];
            int i, j, k;

            //Changed 3 below to 4

            for(i = 0; i < 4; i++) {
            System.out.print("Masukkan array pertama ke-" + i + ": ");
            arr1[i] = sc.nextInt();
            }

            //Changed 3 below to 4;

            for(j = 0; j < 4; j++) {
            System.out.print("Masukkan array kedua ke-" + j + ": ");
            arr2[j] = sc.nextInt();
            }

            for(k = 0; k < 4; k++) {
            int arr3 = new int[3];
            System.out.println(arr3[k] = arr1[i] + arr2[j]);
            }





            share|improve this answer



















            • 1





              this answer is plain wrong. your code would crash on arrayIndexOutOfBounds. The length of the arrays is still 3, while you are going to look for fourth elements

              – Stultuske
              Nov 13 '18 at 12:23











            • Using this data: arr1 = {1, 2, 3, 4} arr2 = {1, 2, 3, 4} arr3 = {2, 4, 6, 8} the length of each array is 4.

              – Jolaosho batmat
              Nov 13 '18 at 12:35













            • int arr1 = new int[3]; int arr2 = new int[3];

              – Stultuske
              Nov 13 '18 at 14:20
















            -1














            The problem is the input length is 3 and the output length is 4.



            Scanner sc = new Scanner(System.in);
            int arr1 = new int[3];
            int arr2 = new int[3];
            int i, j, k;

            //Changed 3 below to 4

            for(i = 0; i < 4; i++) {
            System.out.print("Masukkan array pertama ke-" + i + ": ");
            arr1[i] = sc.nextInt();
            }

            //Changed 3 below to 4;

            for(j = 0; j < 4; j++) {
            System.out.print("Masukkan array kedua ke-" + j + ": ");
            arr2[j] = sc.nextInt();
            }

            for(k = 0; k < 4; k++) {
            int arr3 = new int[3];
            System.out.println(arr3[k] = arr1[i] + arr2[j]);
            }





            share|improve this answer



















            • 1





              this answer is plain wrong. your code would crash on arrayIndexOutOfBounds. The length of the arrays is still 3, while you are going to look for fourth elements

              – Stultuske
              Nov 13 '18 at 12:23











            • Using this data: arr1 = {1, 2, 3, 4} arr2 = {1, 2, 3, 4} arr3 = {2, 4, 6, 8} the length of each array is 4.

              – Jolaosho batmat
              Nov 13 '18 at 12:35













            • int arr1 = new int[3]; int arr2 = new int[3];

              – Stultuske
              Nov 13 '18 at 14:20














            -1












            -1








            -1







            The problem is the input length is 3 and the output length is 4.



            Scanner sc = new Scanner(System.in);
            int arr1 = new int[3];
            int arr2 = new int[3];
            int i, j, k;

            //Changed 3 below to 4

            for(i = 0; i < 4; i++) {
            System.out.print("Masukkan array pertama ke-" + i + ": ");
            arr1[i] = sc.nextInt();
            }

            //Changed 3 below to 4;

            for(j = 0; j < 4; j++) {
            System.out.print("Masukkan array kedua ke-" + j + ": ");
            arr2[j] = sc.nextInt();
            }

            for(k = 0; k < 4; k++) {
            int arr3 = new int[3];
            System.out.println(arr3[k] = arr1[i] + arr2[j]);
            }





            share|improve this answer













            The problem is the input length is 3 and the output length is 4.



            Scanner sc = new Scanner(System.in);
            int arr1 = new int[3];
            int arr2 = new int[3];
            int i, j, k;

            //Changed 3 below to 4

            for(i = 0; i < 4; i++) {
            System.out.print("Masukkan array pertama ke-" + i + ": ");
            arr1[i] = sc.nextInt();
            }

            //Changed 3 below to 4;

            for(j = 0; j < 4; j++) {
            System.out.print("Masukkan array kedua ke-" + j + ": ");
            arr2[j] = sc.nextInt();
            }

            for(k = 0; k < 4; k++) {
            int arr3 = new int[3];
            System.out.println(arr3[k] = arr1[i] + arr2[j]);
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 12:20









            Jolaosho batmatJolaosho batmat

            195




            195








            • 1





              this answer is plain wrong. your code would crash on arrayIndexOutOfBounds. The length of the arrays is still 3, while you are going to look for fourth elements

              – Stultuske
              Nov 13 '18 at 12:23











            • Using this data: arr1 = {1, 2, 3, 4} arr2 = {1, 2, 3, 4} arr3 = {2, 4, 6, 8} the length of each array is 4.

              – Jolaosho batmat
              Nov 13 '18 at 12:35













            • int arr1 = new int[3]; int arr2 = new int[3];

              – Stultuske
              Nov 13 '18 at 14:20














            • 1





              this answer is plain wrong. your code would crash on arrayIndexOutOfBounds. The length of the arrays is still 3, while you are going to look for fourth elements

              – Stultuske
              Nov 13 '18 at 12:23











            • Using this data: arr1 = {1, 2, 3, 4} arr2 = {1, 2, 3, 4} arr3 = {2, 4, 6, 8} the length of each array is 4.

              – Jolaosho batmat
              Nov 13 '18 at 12:35













            • int arr1 = new int[3]; int arr2 = new int[3];

              – Stultuske
              Nov 13 '18 at 14:20








            1




            1





            this answer is plain wrong. your code would crash on arrayIndexOutOfBounds. The length of the arrays is still 3, while you are going to look for fourth elements

            – Stultuske
            Nov 13 '18 at 12:23





            this answer is plain wrong. your code would crash on arrayIndexOutOfBounds. The length of the arrays is still 3, while you are going to look for fourth elements

            – Stultuske
            Nov 13 '18 at 12:23













            Using this data: arr1 = {1, 2, 3, 4} arr2 = {1, 2, 3, 4} arr3 = {2, 4, 6, 8} the length of each array is 4.

            – Jolaosho batmat
            Nov 13 '18 at 12:35







            Using this data: arr1 = {1, 2, 3, 4} arr2 = {1, 2, 3, 4} arr3 = {2, 4, 6, 8} the length of each array is 4.

            – Jolaosho batmat
            Nov 13 '18 at 12:35















            int arr1 = new int[3]; int arr2 = new int[3];

            – Stultuske
            Nov 13 '18 at 14:20





            int arr1 = new int[3]; int arr2 = new int[3];

            – Stultuske
            Nov 13 '18 at 14:20


















            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%2f53280742%2fsumming-2-arrays-to-third-array%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

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation