How to Create a new array each cycle of a for loop











up vote
-2
down vote

favorite












Hello im looking at how to create a new array and store the two values obtained from the user each iteration of the for loop. Since it goes from 1-4 i want to create 4 arrays and each time it appends the values the user gives in that iteration. If it would be better to create one big array that stores all the values, please share however I would like to keep the code simple. The example below shows my code, the array part is the part im having issues with at the moment.



import java.util.Scanner;

public class Arrays{

public static void main(String args)

String array1;
String array2;
String array3;
String array4;

for (int i=1; i <5; i++) {

System.out.println("What is track " + i);
Scanner sc = new Scanner(System.in);
String track = sc.nextLine();
System.out.println("How many thousands of times has it been downloaded? ");
Scanner sd = new Scanner(System.in);
String time = sd.nextLine();
array1[0]=track;
array1[1]=time;

}
System.out.println(arrays1);
System.out.println(arrays2);
System.out.println(arrays3);
System.out.println(arrays4);
}









share|improve this question
























  • Create an array that contain your 4 arrays, String[4][2]
    – Joakim Danielson
    Nov 19 at 21:47










  • You shouldn't be using arrays to store different fields. Use a proper class.
    – shmosel
    Nov 19 at 21:57










  • I would really appreciate if you can show me an example
    – Mohamed Ismail
    Nov 19 at 22:04















up vote
-2
down vote

favorite












Hello im looking at how to create a new array and store the two values obtained from the user each iteration of the for loop. Since it goes from 1-4 i want to create 4 arrays and each time it appends the values the user gives in that iteration. If it would be better to create one big array that stores all the values, please share however I would like to keep the code simple. The example below shows my code, the array part is the part im having issues with at the moment.



import java.util.Scanner;

public class Arrays{

public static void main(String args)

String array1;
String array2;
String array3;
String array4;

for (int i=1; i <5; i++) {

System.out.println("What is track " + i);
Scanner sc = new Scanner(System.in);
String track = sc.nextLine();
System.out.println("How many thousands of times has it been downloaded? ");
Scanner sd = new Scanner(System.in);
String time = sd.nextLine();
array1[0]=track;
array1[1]=time;

}
System.out.println(arrays1);
System.out.println(arrays2);
System.out.println(arrays3);
System.out.println(arrays4);
}









share|improve this question
























  • Create an array that contain your 4 arrays, String[4][2]
    – Joakim Danielson
    Nov 19 at 21:47










  • You shouldn't be using arrays to store different fields. Use a proper class.
    – shmosel
    Nov 19 at 21:57










  • I would really appreciate if you can show me an example
    – Mohamed Ismail
    Nov 19 at 22:04













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











Hello im looking at how to create a new array and store the two values obtained from the user each iteration of the for loop. Since it goes from 1-4 i want to create 4 arrays and each time it appends the values the user gives in that iteration. If it would be better to create one big array that stores all the values, please share however I would like to keep the code simple. The example below shows my code, the array part is the part im having issues with at the moment.



import java.util.Scanner;

public class Arrays{

public static void main(String args)

String array1;
String array2;
String array3;
String array4;

for (int i=1; i <5; i++) {

System.out.println("What is track " + i);
Scanner sc = new Scanner(System.in);
String track = sc.nextLine();
System.out.println("How many thousands of times has it been downloaded? ");
Scanner sd = new Scanner(System.in);
String time = sd.nextLine();
array1[0]=track;
array1[1]=time;

}
System.out.println(arrays1);
System.out.println(arrays2);
System.out.println(arrays3);
System.out.println(arrays4);
}









share|improve this question















Hello im looking at how to create a new array and store the two values obtained from the user each iteration of the for loop. Since it goes from 1-4 i want to create 4 arrays and each time it appends the values the user gives in that iteration. If it would be better to create one big array that stores all the values, please share however I would like to keep the code simple. The example below shows my code, the array part is the part im having issues with at the moment.



import java.util.Scanner;

public class Arrays{

public static void main(String args)

String array1;
String array2;
String array3;
String array4;

for (int i=1; i <5; i++) {

System.out.println("What is track " + i);
Scanner sc = new Scanner(System.in);
String track = sc.nextLine();
System.out.println("How many thousands of times has it been downloaded? ");
Scanner sd = new Scanner(System.in);
String time = sd.nextLine();
array1[0]=track;
array1[1]=time;

}
System.out.println(arrays1);
System.out.println(arrays2);
System.out.println(arrays3);
System.out.println(arrays4);
}






java arrays for-loop java.util.scanner






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 21:53









azro

9,77941438




9,77941438










asked Nov 19 at 21:36









Mohamed Ismail

75




75












  • Create an array that contain your 4 arrays, String[4][2]
    – Joakim Danielson
    Nov 19 at 21:47










  • You shouldn't be using arrays to store different fields. Use a proper class.
    – shmosel
    Nov 19 at 21:57










  • I would really appreciate if you can show me an example
    – Mohamed Ismail
    Nov 19 at 22:04


















  • Create an array that contain your 4 arrays, String[4][2]
    – Joakim Danielson
    Nov 19 at 21:47










  • You shouldn't be using arrays to store different fields. Use a proper class.
    – shmosel
    Nov 19 at 21:57










  • I would really appreciate if you can show me an example
    – Mohamed Ismail
    Nov 19 at 22:04
















Create an array that contain your 4 arrays, String[4][2]
– Joakim Danielson
Nov 19 at 21:47




Create an array that contain your 4 arrays, String[4][2]
– Joakim Danielson
Nov 19 at 21:47












You shouldn't be using arrays to store different fields. Use a proper class.
– shmosel
Nov 19 at 21:57




You shouldn't be using arrays to store different fields. Use a proper class.
– shmosel
Nov 19 at 21:57












I would really appreciate if you can show me an example
– Mohamed Ismail
Nov 19 at 22:04




I would really appreciate if you can show me an example
– Mohamed Ismail
Nov 19 at 22:04












2 Answers
2






active

oldest

votes

















up vote
0
down vote













(1) Use a 2 dimensional array

(2) Use 1 Scanner object



String array = new String[4][2];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < array.length; i++) {
System.out.println("What is track " + (i + 1));
array[i][0] = sc.nextLine();
System.out.println("How many thousands of times has it been downloaded? ");
array[i][1] = sc.nextLine();
}
sc.close();

for (int i = 0; i < array.length; i++) {
System.out.print("track" + (i + 1) + " = " + array[i][0]);
System.out.println(", downloaded = " + array[i][1]);
}





share|improve this answer




























    up vote
    0
    down vote













    You can use a 2-dimensional array, in this case you will have an array of size 4, where each index will hold an array of size two. You can then use the value of i in your for loop to access the outer arrays. I would also suggest using just 1 scanner, and putting it outside of your loop as there is no need to create a new scanner for every iteration of your for loop. You should also try to avoid hard-coding integer values into the code and use variables since these values can be subject to change at later time. If you wanted to get for example 6 sets of values from the user, you would then have to go through your code and manually change all entries of 4 as opposed to just using a variable for this and changing the variable value.



    import java.util.Scanner;
    import java.util.Arrays;

    public class Arrays1{

    public static void main(String args){

    String array = new String[4][2];
    Scanner sc = new Scanner(System.in);

    for (int i = 0; i < 4; i++) {

    System.out.println("What is track " + i + 1);
    array[i][0] = sc.nextLine();
    System.out.println("How many thousands of times has it been downloaded? ");
    array[i][1] = sc.nextLine();
    }

    sc.close();

    // More elegant to loop here and print these values instead of hardcoding the indices
    System.out.println(Arrays.toString(array[0]));
    System.out.println(Arrays.toString(array[1]));
    System.out.println(Arrays.toString(array[2]));
    System.out.println(Arrays.toString(array[3]));
    }
    }





    share|improve this answer























    • You should also try to avoid hard-coding integer values how many changes will you have to do in the above code if the number changes from 4 to 6?
      – forpas
      Nov 19 at 22:26










    • @forpas This was intentional to illustrate the advice using the example of 4 and the loop comment. Feel free to edit as needed if you believe a variable example would serve better
      – M.G
      Nov 19 at 22:35










    • I don't need to edit your answer because you do and after each edit (there were several) it comes closer and closer to my code but still you have work to do.
      – forpas
      Nov 19 at 22:38











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382983%2fhow-to-create-a-new-array-each-cycle-of-a-for-loop%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    (1) Use a 2 dimensional array

    (2) Use 1 Scanner object



    String array = new String[4][2];
    Scanner sc = new Scanner(System.in);
    for (int i = 0; i < array.length; i++) {
    System.out.println("What is track " + (i + 1));
    array[i][0] = sc.nextLine();
    System.out.println("How many thousands of times has it been downloaded? ");
    array[i][1] = sc.nextLine();
    }
    sc.close();

    for (int i = 0; i < array.length; i++) {
    System.out.print("track" + (i + 1) + " = " + array[i][0]);
    System.out.println(", downloaded = " + array[i][1]);
    }





    share|improve this answer

























      up vote
      0
      down vote













      (1) Use a 2 dimensional array

      (2) Use 1 Scanner object



      String array = new String[4][2];
      Scanner sc = new Scanner(System.in);
      for (int i = 0; i < array.length; i++) {
      System.out.println("What is track " + (i + 1));
      array[i][0] = sc.nextLine();
      System.out.println("How many thousands of times has it been downloaded? ");
      array[i][1] = sc.nextLine();
      }
      sc.close();

      for (int i = 0; i < array.length; i++) {
      System.out.print("track" + (i + 1) + " = " + array[i][0]);
      System.out.println(", downloaded = " + array[i][1]);
      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        (1) Use a 2 dimensional array

        (2) Use 1 Scanner object



        String array = new String[4][2];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < array.length; i++) {
        System.out.println("What is track " + (i + 1));
        array[i][0] = sc.nextLine();
        System.out.println("How many thousands of times has it been downloaded? ");
        array[i][1] = sc.nextLine();
        }
        sc.close();

        for (int i = 0; i < array.length; i++) {
        System.out.print("track" + (i + 1) + " = " + array[i][0]);
        System.out.println(", downloaded = " + array[i][1]);
        }





        share|improve this answer












        (1) Use a 2 dimensional array

        (2) Use 1 Scanner object



        String array = new String[4][2];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < array.length; i++) {
        System.out.println("What is track " + (i + 1));
        array[i][0] = sc.nextLine();
        System.out.println("How many thousands of times has it been downloaded? ");
        array[i][1] = sc.nextLine();
        }
        sc.close();

        for (int i = 0; i < array.length; i++) {
        System.out.print("track" + (i + 1) + " = " + array[i][0]);
        System.out.println(", downloaded = " + array[i][1]);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 22:09









        forpas

        5,1861217




        5,1861217
























            up vote
            0
            down vote













            You can use a 2-dimensional array, in this case you will have an array of size 4, where each index will hold an array of size two. You can then use the value of i in your for loop to access the outer arrays. I would also suggest using just 1 scanner, and putting it outside of your loop as there is no need to create a new scanner for every iteration of your for loop. You should also try to avoid hard-coding integer values into the code and use variables since these values can be subject to change at later time. If you wanted to get for example 6 sets of values from the user, you would then have to go through your code and manually change all entries of 4 as opposed to just using a variable for this and changing the variable value.



            import java.util.Scanner;
            import java.util.Arrays;

            public class Arrays1{

            public static void main(String args){

            String array = new String[4][2];
            Scanner sc = new Scanner(System.in);

            for (int i = 0; i < 4; i++) {

            System.out.println("What is track " + i + 1);
            array[i][0] = sc.nextLine();
            System.out.println("How many thousands of times has it been downloaded? ");
            array[i][1] = sc.nextLine();
            }

            sc.close();

            // More elegant to loop here and print these values instead of hardcoding the indices
            System.out.println(Arrays.toString(array[0]));
            System.out.println(Arrays.toString(array[1]));
            System.out.println(Arrays.toString(array[2]));
            System.out.println(Arrays.toString(array[3]));
            }
            }





            share|improve this answer























            • You should also try to avoid hard-coding integer values how many changes will you have to do in the above code if the number changes from 4 to 6?
              – forpas
              Nov 19 at 22:26










            • @forpas This was intentional to illustrate the advice using the example of 4 and the loop comment. Feel free to edit as needed if you believe a variable example would serve better
              – M.G
              Nov 19 at 22:35










            • I don't need to edit your answer because you do and after each edit (there were several) it comes closer and closer to my code but still you have work to do.
              – forpas
              Nov 19 at 22:38















            up vote
            0
            down vote













            You can use a 2-dimensional array, in this case you will have an array of size 4, where each index will hold an array of size two. You can then use the value of i in your for loop to access the outer arrays. I would also suggest using just 1 scanner, and putting it outside of your loop as there is no need to create a new scanner for every iteration of your for loop. You should also try to avoid hard-coding integer values into the code and use variables since these values can be subject to change at later time. If you wanted to get for example 6 sets of values from the user, you would then have to go through your code and manually change all entries of 4 as opposed to just using a variable for this and changing the variable value.



            import java.util.Scanner;
            import java.util.Arrays;

            public class Arrays1{

            public static void main(String args){

            String array = new String[4][2];
            Scanner sc = new Scanner(System.in);

            for (int i = 0; i < 4; i++) {

            System.out.println("What is track " + i + 1);
            array[i][0] = sc.nextLine();
            System.out.println("How many thousands of times has it been downloaded? ");
            array[i][1] = sc.nextLine();
            }

            sc.close();

            // More elegant to loop here and print these values instead of hardcoding the indices
            System.out.println(Arrays.toString(array[0]));
            System.out.println(Arrays.toString(array[1]));
            System.out.println(Arrays.toString(array[2]));
            System.out.println(Arrays.toString(array[3]));
            }
            }





            share|improve this answer























            • You should also try to avoid hard-coding integer values how many changes will you have to do in the above code if the number changes from 4 to 6?
              – forpas
              Nov 19 at 22:26










            • @forpas This was intentional to illustrate the advice using the example of 4 and the loop comment. Feel free to edit as needed if you believe a variable example would serve better
              – M.G
              Nov 19 at 22:35










            • I don't need to edit your answer because you do and after each edit (there were several) it comes closer and closer to my code but still you have work to do.
              – forpas
              Nov 19 at 22:38













            up vote
            0
            down vote










            up vote
            0
            down vote









            You can use a 2-dimensional array, in this case you will have an array of size 4, where each index will hold an array of size two. You can then use the value of i in your for loop to access the outer arrays. I would also suggest using just 1 scanner, and putting it outside of your loop as there is no need to create a new scanner for every iteration of your for loop. You should also try to avoid hard-coding integer values into the code and use variables since these values can be subject to change at later time. If you wanted to get for example 6 sets of values from the user, you would then have to go through your code and manually change all entries of 4 as opposed to just using a variable for this and changing the variable value.



            import java.util.Scanner;
            import java.util.Arrays;

            public class Arrays1{

            public static void main(String args){

            String array = new String[4][2];
            Scanner sc = new Scanner(System.in);

            for (int i = 0; i < 4; i++) {

            System.out.println("What is track " + i + 1);
            array[i][0] = sc.nextLine();
            System.out.println("How many thousands of times has it been downloaded? ");
            array[i][1] = sc.nextLine();
            }

            sc.close();

            // More elegant to loop here and print these values instead of hardcoding the indices
            System.out.println(Arrays.toString(array[0]));
            System.out.println(Arrays.toString(array[1]));
            System.out.println(Arrays.toString(array[2]));
            System.out.println(Arrays.toString(array[3]));
            }
            }





            share|improve this answer














            You can use a 2-dimensional array, in this case you will have an array of size 4, where each index will hold an array of size two. You can then use the value of i in your for loop to access the outer arrays. I would also suggest using just 1 scanner, and putting it outside of your loop as there is no need to create a new scanner for every iteration of your for loop. You should also try to avoid hard-coding integer values into the code and use variables since these values can be subject to change at later time. If you wanted to get for example 6 sets of values from the user, you would then have to go through your code and manually change all entries of 4 as opposed to just using a variable for this and changing the variable value.



            import java.util.Scanner;
            import java.util.Arrays;

            public class Arrays1{

            public static void main(String args){

            String array = new String[4][2];
            Scanner sc = new Scanner(System.in);

            for (int i = 0; i < 4; i++) {

            System.out.println("What is track " + i + 1);
            array[i][0] = sc.nextLine();
            System.out.println("How many thousands of times has it been downloaded? ");
            array[i][1] = sc.nextLine();
            }

            sc.close();

            // More elegant to loop here and print these values instead of hardcoding the indices
            System.out.println(Arrays.toString(array[0]));
            System.out.println(Arrays.toString(array[1]));
            System.out.println(Arrays.toString(array[2]));
            System.out.println(Arrays.toString(array[3]));
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 at 22:21

























            answered Nov 19 at 21:57









            M.G

            408310




            408310












            • You should also try to avoid hard-coding integer values how many changes will you have to do in the above code if the number changes from 4 to 6?
              – forpas
              Nov 19 at 22:26










            • @forpas This was intentional to illustrate the advice using the example of 4 and the loop comment. Feel free to edit as needed if you believe a variable example would serve better
              – M.G
              Nov 19 at 22:35










            • I don't need to edit your answer because you do and after each edit (there were several) it comes closer and closer to my code but still you have work to do.
              – forpas
              Nov 19 at 22:38


















            • You should also try to avoid hard-coding integer values how many changes will you have to do in the above code if the number changes from 4 to 6?
              – forpas
              Nov 19 at 22:26










            • @forpas This was intentional to illustrate the advice using the example of 4 and the loop comment. Feel free to edit as needed if you believe a variable example would serve better
              – M.G
              Nov 19 at 22:35










            • I don't need to edit your answer because you do and after each edit (there were several) it comes closer and closer to my code but still you have work to do.
              – forpas
              Nov 19 at 22:38
















            You should also try to avoid hard-coding integer values how many changes will you have to do in the above code if the number changes from 4 to 6?
            – forpas
            Nov 19 at 22:26




            You should also try to avoid hard-coding integer values how many changes will you have to do in the above code if the number changes from 4 to 6?
            – forpas
            Nov 19 at 22:26












            @forpas This was intentional to illustrate the advice using the example of 4 and the loop comment. Feel free to edit as needed if you believe a variable example would serve better
            – M.G
            Nov 19 at 22:35




            @forpas This was intentional to illustrate the advice using the example of 4 and the loop comment. Feel free to edit as needed if you believe a variable example would serve better
            – M.G
            Nov 19 at 22:35












            I don't need to edit your answer because you do and after each edit (there were several) it comes closer and closer to my code but still you have work to do.
            – forpas
            Nov 19 at 22:38




            I don't need to edit your answer because you do and after each edit (there were several) it comes closer and closer to my code but still you have work to do.
            – forpas
            Nov 19 at 22:38


















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53382983%2fhow-to-create-a-new-array-each-cycle-of-a-for-loop%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python