0

I'm a beginner in java. I have question about for each. can I use it in order to get input from user? If yes, How?

I tried this code, but didn't work:

Scanner scanner = new Scanner(System.in);
int[] arr = new int[3];
for(int m: arr){
    m = scanner.nextInt();
 }
//arr[0] = 1 , arr[1] = 5 , arr[2] = 61

But when I printed my array:
0 0 0
was shown.

F.Rajabi
  • 13
  • 4
  • https://stackoverflow.com/questions/477550/is-there-a-way-to-access-an-iteration-counter-in-javas-for-each-loop – Luk Feb 14 '19 at 13:50
  • 1
    Changing the value of `m` will not modify the element in the array. See [Is Java “pass-by-reference” or “pass-by-value”?](https://stackoverflow.com/questions/40480/). – Slaw Feb 14 '19 at 13:50
  • The enhanced for loop (what you call *for each*) only takes elements out of the array, collection or iterable that you’re iterating. It cannot put anything back there. So it seems you are asking the impossible. – Ole V.V. Feb 14 '19 at 14:03

5 Answers5

1

You should use a for-loop with index for that, in order to write into the array:

    Scanner scanner = new Scanner(System.in);
    int[] arr = new int[3];
    for(int i=0;i<arr.length;i++)
    {
        arr[i]=scanner.nextInt();
    }

Currently what you are doing with the for-each is getting the value of m from the array, set it to the user input, but you are not adding it back into the array. Therefore your array remains empty.

vs97
  • 5,765
  • 3
  • 28
  • 41
1

int[] arr = new int[3]; sets up an array of 3 0s.

Within each iteration of your loop, m = scanner.nextInt(); just sets m to whatever the input is, and adds nothing to the array.

Do you specifically need to assign values within a foreach loop?

If not, then just assign a position in the array using a standard for loop to do it this way -> for (int i = 0; i < arr.length; i++) { and assign with arr[i]= scanner.nextInt();.

If so, I think you'll need to set up your own iteration counter, and use that.

achAmháin
  • 4,176
  • 4
  • 17
  • 40
0

m is being declared as a local int and assigned, but the array is not being updated. You need to set arr[x] equal to the next int of the scanner. Right now you're just reading it, assigning it to m, and doing nothing with it.

for(int i=0; i<arr.length; i++){
        arr[i]=scanner.nextInt();

}
M. Goodman
  • 141
  • 9
0

I'm Shouldn't recommend this solution, you just use for loop to get the input from the user.

Scanner scanner = new Scanner(System.in);
int [] arr = new int[3];
int k = 0;
for(int m : arr) {
    arr[k] = scanner.nextInt();
    ++k;
}
scanner.close();

now, it will work
//arr[0] = 1 , arr[1] = 5 , arr[2] = 61

Ramesh NR
  • 1
  • 2
0
static void foreach()
{
    int amount;
    
    Scanner scanner = new Scanner(System.in);       
    
    System.out.print("amount in arr : ");
    amount = scanner.nextInt();
    
    int arr[] = new int[amount];
    
    for(int newVar : arr)
    {
        System.out.print("[" + (newVar +1) +"] = " );
        newVar = scanner.nextInt();
    }