After initializing an array and using the advanced for loop to capture 2 input values and update in the array, printing the values out results only in the last integer input showing, and a zero.
<code>
int[] testArray = new int[2];
Scanner input = new Scanner(System.in);
for(int val : testArray)
{
testArray[val] = input.nextInt();
}
for(int x = 0; x < testArray.length; ++x){
System.out.println(testArray[x]);
</code>
<code>
int[] testArray = new int[2];
Scanner input = new Scanner(System.in);
for(int val : testArray)
{
testArray[val] = input.nextInt();
}
for(int x = 0; x < testArray.length; ++x){
System.out.println(testArray[x]);
</code>
int[] testArray = new int[2];
Scanner input = new Scanner(System.in);
for(int val : testArray)
{
testArray[val] = input.nextInt();
}
for(int x = 0; x < testArray.length; ++x){
System.out.println(testArray[x]);
Using the normal for loop works when capturing input. Not sure what is going on logic wise with the advanced for loop.
New contributor
Ken Kiarie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.