-2

What I'm trying to achieve is:

  1. Create an array, and define its length with user input (scanner). ✓
  2. Loop through the array to fill in its values. ✓
  3. After defining its length and filling in the values according to the length, printing the whole array. X

I wasn't able to achieve number 3. Can someone help me? I simply need to print the array.

This is my code:

package arrayinputoutput;

import java.util.Scanner;

public class ArrayInputOutput {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x;
        int[] test;

        System.out.println("How long should the array be?");
        x = input.nextInt();

        for (int i = 0; i < x + 1; i++) {
            input.nextLine();
            System.out.println("Please fill in position " + i + ":");
            i = input.nextInt();

        }
        //System.out.println(test[]);
    }
}
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
  • But you're not filling the array!! As you read each value, you have to fill the array on each index. – Renato Afonso Sep 25 '17 at 16:17
  • @RenatoAfonso That's not the real problem; this code doesn't even compile. – progyammer Sep 25 '17 at 16:36
  • @progyammer First of all from what do you assume it doesn't compile? it compiles just FINE. – Deniz Yildiz Sep 25 '17 at 21:48
  • Why it doesn't compile? I think it compiles, although it is not doing half of what he says, but it compiles.... :) – Renato Afonso Sep 26 '17 at 08:08
  • I'm sorry. Your code does compile. The thing is, you have not initialized the array `test`. In this state, if you try to access any index of that array, it would not compile. But you have not used the array (which is what I failed to notice), so no compiler error is produced. My bad. :-p – progyammer Sep 26 '17 at 10:03

2 Answers2

2
  • after asking the size, yo have to create the array, with the size
  • you have to store the input in a box of the array myArray[i] = input.nextInt(); then skip a line
  • please use explicit name for variables
  • stop to size and not size+1 because indexes start to 0

So like this :

int size;
int[] myArray;
System.out.println("How long should the array be?");
size = input.nextInt();
input.nextLine();
myArray= new int[size];                     //<- create array

for (int i = 0; i < size ; i++) {           // <- '<size' and '<size+1'
    System.out.println("Please fill in position " + i + ":");
    myArray[i] = input.nextInt();           // <- store in a box of the array
    input.nextLine();
}

To print the array, several options like :

System.out.println(Arrays.toString(myArray));

OR :

for (int i = 0; i < size ; i++) {          
    System.out.println("Value in position " + i + ":" + myArray[i]);       
}
azro
  • 53,056
  • 7
  • 34
  • 70
  • I have to do Size + 1 because, if i input an array length of 5, it will ask only 4. That's why. – Deniz Yildiz Sep 27 '17 at 11:03
  • @DenizYildiz sure BUT you start to 0, that's how array works, look here [Documentation](https://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#jls-10.4) if you set size+1 you'll have OutOfBoundsException – azro Sep 27 '17 at 17:00
0

System.out.println(Arrays.toString(test));

Try using this to print the array.. Hope it works