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);
}
java arrays for-loop java.util.scanner
add a comment |
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);
}
java arrays for-loop java.util.scanner
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
add a comment |
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);
}
java arrays for-loop java.util.scanner
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
java arrays for-loop java.util.scanner
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
add a comment |
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
add a comment |
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]);
}
add a comment |
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]));
}
}
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
add a comment |
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]);
}
add a comment |
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]);
}
add a comment |
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]);
}
(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]);
}
answered Nov 19 at 22:09
forpas
5,1861217
5,1861217
add a comment |
add a comment |
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]));
}
}
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
add a comment |
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]));
}
}
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
add a comment |
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]));
}
}
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]));
}
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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