0

So i'm having a hard time, finding ways on how to put integers inside the array, my objective is, make a program that a user will store 10 numbers and then after that display it.

So this is what I've done so far:

import java.util.Scanner;

public class wtf {
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        int array[] = new int[10];

        for(int i=1;i<array.length;i++){
            System.out.print("Enter a number: ");
            array[] = input.nextInt();
        }
    }
}

But unfortunately there is an error.

1
  • array[] is wrong , you have to metion which index. array[i] Commented Jun 15, 2016 at 6:31

4 Answers 4

1

Firstly you need to specify the index of where you want to insert the data

which in you case would be

 array[i - 1] = input.nextInt();

As arrays are zero based it would be better to loop like

for(int i=0; i<array.length; i++){
....
Sign up to request clarification or add additional context in comments.

Comments

0

You are missing the index when you access the array.

What you need is:

// You should also start from i = 0
for(int i=0;i<array.length;i++){
    System.out.print("Enter a number: ");

    // need array[i] here
    array[i] = input.nextInt();
}

If you want to display the number after, you can do:

for (int i = 0; i < array.length; i++)
{
    System.out.println(array[i]);
}

Comments

0

You can't add elements to array, you just override existing ones. So you need to provide the index where to put the element.

You should change a line:

        array[i-1] = input.nextInt();

i-1 because arrays are indexed from 0 in java, and your fors i is from 1 to 10.

More over you are iterating between 1 and 9 (i = 1, i < array.length), you should use code below to iterate between 1 and 10:

for(int i=1;i<=array.length;i++){

So basically you should use one of the blocks below:

1-indexed

for(int i=1;i<=array.length;i++){
    System.out.print("Enter a number: ");
    array[i-1] = input.nextInt();
    }
}

0-indexed

for(int i=0;i<array.length;i++){
    System.out.print("Enter a number: ");
    array[i] = input.nextInt();
    }
}

Another option would be to use a collection, e.g. ArrayList in this case you wouldn't need to provide up front the size of it.

1 Comment

Thank you very much, now i know how to index arrays properly :)
-1

First of all you need to specify an index for the integer array object... every element of the array has its specific index... starting from 0 to n-1 where n is the number of elements in the array.. second of all, the index in programming always starts from 0 Index is often confused with position.. Position has its usual meaning starting from 1... consider an array having elements as 56 98 65 12

here 98 is at index 1 and at position 2

all u need to do now is

change the input line to:

array[i]=input.nextInt();

1 Comment

the index in programming always starts from 0 Index - this is incorrect

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.