2

I've been up for a few hours trying to find a solution.

My program asks the user to enter a list of integers (example: 5 2 5 6 6 1). Then I would like to create an array and store each integer into its respective array index, consecutively.

Here is the part of my program i'm having trouble with (this program was meant to perform calculations via a method later on, but I didn't include that):

import java.util.Scanner;
public class Assignment627 {

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

    int x = 0;
    int[] list1Array = new int[1];

    System.out.println("Enter list1: ");
    while (input.hasNext()){
        list1Array[x] = input.nextInt();
        x++;
    }

As you can see, I am instantiating the array "list1Array" but the problem is I don't know how many integers the user would enter! If only there were a way of knowing how many integers have been input... Any help would be greatly appreciated! Thanks, Sebastian

seba1685
  • 87
  • 1
  • 10
  • 3
    use `ArrayList` instead of int[] – Jens Nov 05 '14 at 08:38
  • `int[] list1Array = new int[1];` is not gonna work. Use a `List list1Array = new List();`. It will create an empty list for you where you can add data to it based on index. Read about List in Java. – ha9u63a7 Nov 05 '14 at 08:40
  • 2
    @hagubear you mean `List list = new ArrayList();`, your answer is **C#** – EpicPandaForce Nov 05 '14 at 08:41
  • @EpicPandaForce I forgot that some Collection implementations take the class-types, not primitives. Thanks for that +1. – ha9u63a7 Nov 05 '14 at 08:44

5 Answers5

5
import java.util.Scanner;
public class Assignment627 {

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

        //int[] list1Array = new int[1];
        List<Integer> list1Array = new ArrayList<>();

        System.out.println("Enter list1: ");
        while (input.hasNext()){
            list1Array.add(input.nextInt());
        }   
    }

}

An ArrayList, is like an array that does not have a predefined size and it dynamically changes its size; exactly what you are looking for. You can get its size by list1Array.size();

If you insist on having the final result as an array, then you can later call the toArray() method of ArrayList. This post will be helpful.

Community
  • 1
  • 1
vefthym
  • 7,422
  • 6
  • 32
  • 58
2

If you really want to use Arrays, do it like this:

import java.util.Scanner;

public class Assignment627 {

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

        int x = 0;
        int[] list1Array = new int[1];

        System.out.println("Enter list1: ");
        while (input.hasNext()) {
            list1Array[x] = input.nextInt();
            x++;
            int[] temp = new int[list1Array.length + 1];

            for (int i = 0; i < list1Array.length; i++) {
                temp[i] = list1Array[i];
            }

            list1Array = temp;
        }
    }
}
Sujith Thankachan
  • 3,508
  • 2
  • 20
  • 25
1
 public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter elemnt size ");
        int size = input.nextInt();

        int x = 0;
        int[] list1Array = new int[size];

        for (int y = 0 ; y < size ; y++) {
            System.out.println("Enter number");
            list1Array[x] = input.nextInt();
            x++;
        }

        System.out.println(Arrays.toString(list1Array));
    }

Output

Enter elemnt size 
4
Enter number
2
Enter number
3
Enter number
4
Enter number
2
[2, 3, 4, 2]
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
1

Use List, in your case ArrayList will be fine:

List<Integer> list1Array = new ArrayList();

while (input.hasNext()){
    list1Array.add(input.nextInt());
    x++;
}
1ac0
  • 2,875
  • 3
  • 33
  • 47
1

You should take the input as string and then use string.split(" "); and then obtain an array of Strings representing each number. Obtaining array by string can be done by string tokenizing.

UPDATE

But you should be careful not to put other chars as separators for numbers number

Adrian Totolici
  • 223
  • 2
  • 20