0

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]);
            }
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Elec Hill
  • 19
  • 3
  • 4
    "*What is happening to input.nextInt(); when I set array[j] equal to input.nextInt(); ?*" since `input` is `Scanner input = new Scanner(System.in);` it reads values from `System.in` which most likely represents input stream from console. So `array[j] = input.nextInt();` would wait for you to provide some integer value via console and assign that value to `array[j]` *each time it is executed*, so for array of length 4 you need to provide 4 separate values. – Pshemo Aug 14 '21 at 19:44
  • 4
    "*I'm asking because I thought it would equal 4 if I declared the length of the array with the scanner method*" could you clarify (1) what you mean in that sentence, and (2) what makes you think so? – Pshemo Aug 14 '21 at 19:45
  • 2
    I don't understand the question. You create an array, then add a value to it.. What's the problem? Please explain. – avocadoLambda Aug 14 '21 at 19:52
  • I thought that the array length is declared by the scanner input. I'm lost where the array[j] is set equal to the input.nextInt() method in the for loop. I'm confused about if I entered 4 for the array length, why doesn't it carry over to the the array[j] declaration. – Elec Hill Aug 14 '21 at 19:58
  • @p_agon When I declare the array length to the input.nextInt(); method, why doesn't the length carry over to the first for loop where array[j] equals input.nextInt(); ? – Elec Hill Aug 14 '21 at 20:00
  • 2
    Why will it? Your understanding of primitive arrays seems incorrect. array[j] refers to the value of the array's jth index. You have only set the array.length() as 4. You have not given the value 4 to any index inside the array. – Shivam Puri Aug 14 '21 at 20:02
  • @ElecHill I really can't understand your question lol – Crih.exe Aug 14 '21 at 20:02
  • What do you think `array[j]` means? – Pshemo Aug 14 '21 at 20:09
  • @Crih.exe Okay. Let's say I'm entering an array length as a response to the input.nextInt(); THEN I enter the values of the array. Where I'm lost is in the for loop. array[j] is set equal to input.nextInt, which I thought would be what the array length is but it's not. What exactly is the value of input.nextInt in the for loop? – Elec Hill Aug 14 '21 at 20:10
  • @Pshemo I think it means the position of whatever value of j equates to. – Elec Hill Aug 14 '21 at 20:10
  • 1
    `array[j]` is like *variable* inside `array` at index `[j]`. *value* of that variable is initially set to `0` (default value for `int`). So when you do `array[j] = input.nextInt();` in first iteration of `for (int j = 0; j < array.length; j++)` loop `j` is equal to `0`. So above is like `array[0] = input.nextInt();`. In next iteration it is like `array[1] = input.nextInt();`, then `array[2] = input.nextInt();` and finally `array[3] = input.nextInt();`. *Each* `input.nextInt()` call will expect separate value from console which will be *assigned* at those indexes/variables. – Pshemo Aug 14 '21 at 21:05
  • 1
    I am guessing is that your confusion is caused by thinking that each call of `input.nextInt();` will always return *same value*, in your case 4. That is *NOT* the case. When called `input.nextInt();` it lets user write integer value (like 4) and returns that value *once*. When `input.nextInt();` will be called again, it will let user write `int` value again, but this may be different value than 4 and that different value will be returned. From documentation https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextInt-- "Scans the **next** token of the input as an int." – Pshemo Aug 14 '21 at 21:13
  • @Pshemo I got it. input.nextInt(); is the point of insertion. It lets me add a value for each j position and will let me for, less than the array length , times. It increases j by 1 for each iteration. Makes sense now. Thank you. – Elec Hill Aug 14 '21 at 21:16
  • @Pshemo now how would I get it to print the array because plugging in array into sout results in a weird output like [I@8efb846. Things like finding the min and max of the array work but I can't get the method to print out the values in the array. – Elec Hill Aug 14 '21 at 21:19
  • 1
    [What's the simplest way to print a Java array?](https://stackoverflow.com/q/409784), specifically https://stackoverflow.com/a/409812 may interest you since that answer also includes what needs to be imported. – Pshemo Aug 14 '21 at 21:25

2 Answers2

1

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

avocadoLambda
  • 1,332
  • 7
  • 16
  • 33
1

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.

Crih.exe
  • 502
  • 4
  • 16