Elec Hill
Elec Hill

Reputation: 19

Scanner Input Assigned to a For Loop

I want an array length of 4. For the first for loop, I set array[j] equal to input.nextInt();

What is happening to input.nextInt(); when I set array[j] equal to input.nextInt(); ?

I'm asking because I thought it would equal 4 if I declared the length of the array with the scanner method but I must be missing something because it doesn't equal 4.

When I directly set array[j] equal to 4, I'm not able to add values and I get:

Even 4
Even 4
Even 4
Even 4

Is this setting each indexable value to 4?

When I declare the array length through the scanner I'm able to add values and I get:

odd 1
Even 2
odd 3
Even 4

So to sum the question up. Why doesn't input.nextInt(); in the for loop not equal 4 and what exactly does it equal?

Here's my code:

package com.company;

import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("enter array length");
        int inside = input.nextInt();
        int[] array = new int[inside];

        System.out.println("enter values");
        for (int j = 0; j < array.length; j++) {
            array[j] = input.nextInt();
        }

        for (int i = 0; i < array.length; i++)
            if (array[i] % 2 == 0) {
                System.out.println("Even" + " " + array[i]);
            } else {
                System.out.println("odd" + " " + array[i]);
            }
    }
}

Upvotes: 0

Views: 1312

Answers (2)

avocadoLambda
avocadoLambda

Reputation: 1046

What is happening to input.nextInt(); when I set array[j] equal to input.nextInt();?

array[j] = input.nextInt();

Assigns the entered value to the position on the array. This code works correctly:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("enter array length");
        int inside = input.nextInt();
        int[] array = new int[inside];
        System.out.println("enter values");
        for (int j = 0; j < array.length; j++) {
            array[j] = input.nextInt();
        }
        for (int i = 0; i < array.length; i++)
            if (array[i] % 2 == 0) {
                System.out.println("Even" + " " + array[i]);
            } else {
                System.out.println("odd" + " " + array[i]);
            }
    }
}

enter image description here

Upvotes: 2

Crih.exe
Crih.exe

Reputation: 524

I'm not sure I understand but I'll try to explain. Sorry if I seem too specific but then again, I don't think I understand your question correctly. Also sorry to every other programmer reading this LOL, I will write a very obvious and simplified answer.

input.nextInt() is a method which returns an Integer which is the number you enter in the console when you run the program.

Let's go ahead every line of you main():

Scanner input = new Scanner(System.in);

This line create a new instance of Scanner, which is designed to help in the reading of System.in. This one is what you type in the console when you run the program.

System.out.println("enter array length");

This line print on the console the string passed as parameter.

int inside = input.nextInt();

Finally, in this line you first declare an int variable called inside and then, using the =, you are initializing this variable with the return value of input.nextInt() method. When you run this method, your program will wait until you enter a number in the console. Then the method will return the number you typed which will be assigned to your inside variable.

Let's say you entered 5 in the console: now inside is equal to 5.

int[] array = new int[inside];

This line will declare and initlialize an int array 5 long. This means it can contain 5 different values in it. Here's a graphical representation of an array: 1 Think your array as a set of boxes. A box can contain a value, and each box is numbered from 0 to length - 1, in this case is 4 because 5-1=4.

for (int j = 0; j < array.length; j++) {
    // some code here
}

This is a for loop. The code inside the brackets will be ran as many times as the length of the array, in this case 5. A for loop also contain a temporary variable which exists only inside the loop. This variable is an int, it's called j and its initial value is zero. This variable will increase by one on every repeat of the loop.

array[j] = input.nextInt();

This line will set a new value to a box of the array. Futhermore, this line will be executed five times. As I said before, this for loop contain a variable starting from zero increasing on every repetition. Let's say we are in the first repetition. When you call

array[j]

you select the box with index j, which is now zero. After array[j] there is an =, that means you are setting a new value to this box. This new value is input.nextInt(). Now

PAY ATTENTION TO THIS:

When you run input.nextInt() another time, you aren't trying to get the previous value you entered before in the int inside = input.nextInt(); line, but you're asking an another value from the user which needs to be entered in the console.

So if you first entered 5 as length of the array, input.nextInt() will get a new value from the user and you'll get 5 as return of the method only if the user enter it another time. If you want to set the length of the array in every box of the array, you'll need this:

array[j] = inside;

Where inside is your variable declared before.

In the next repetitions of the loop, with your current code, you will ask the user 4 more values.

The rest of the code is just output so I don't have to explain it.

I hope all this hasn't beed in vain LOL.

Upvotes: 1

Related Questions