1

Hi I am a beginner and today I started learning about arrays. I wrote the following working program.

import java.util.Scanner;

    public class Arr {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("enter some numbers");
            int[] x = new int[5];
            x[0] = scanner.nextInt();
            x[1] = scanner.nextInt();
            x[2] = scanner.nextInt();
            x[3] = scanner.nextInt();
            x[4] = scanner.nextInt();
            String string = "the numbers as requested are : ";
            for(int i = 0; i < x.length; i++) {
                System.out.println(string + x[i]);
            }
            scanner.close();
        }
    }

however if my array had 1000 numbers, this process would have become tiresome. Is there an easy way to input numbers without typing input scanner for each package

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Darshan Datta
  • 15
  • 1
  • 4

4 Answers4

1

Don't just hardcode. Use a while loop and with your limit.

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("enter some numbers");
        int[] x = new int[5];
        int j = 0;
        while (scanner.hasNext() && j < limit) { 
            if (scanner.hasNextInt()) {
                x[j] = scanner.nextInt();
                j++;
            }

        }

        String string = "the numbers as requested are : ";
        for (int i = 0; i < x.length; i++) {
            System.out.println(string + x[i]);

        }
        scanner.close();

    }

Where limit is 4 for 5 inputs, limit is 99 for 100 input's .

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

however if my array had 1000 numbers, this process would have become tiresome. Is there an easy way to input numbers without typing input scanner for each package

Yes, by using loops. Use a for-loop or a while loop.

int[] array = new int[5];
for(int x=0; x<array.length; x++) //Prompt as many times as the array size
    array[x] = scanner.nextInt();

Generally, use a for-loop when you are certain how many times it will iterate, and a while loop when you are not certain how many times the loop would run.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • **Remarks:** You can always change it to a while loop with a terminating condition, for example prompt a `(y/n)` for continuation. – user3437460 Sep 14 '15 at 14:42
0

Expanding on the idea of not having hard-coded limits mentioned by @Suresh-Atta I'd look at making use of an ArrayList, see Why is it preferred to use Lists instead of Arrays in Java, to store a dynamic number of items. Doing so means you need some indicator to exit the reading on the input, in this case the word "exit"

import java.util.Scanner;
import java.util.ArrayList;

public class Arr {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("enter some numbers");
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        while (scanner.hasNext()) {        
            if (scanner.hasNextInt()) {
                numbers.add(scanner.nextInt());
            } else {
                String s1 = scanner.next();
                if ("exit".equalsIgnoreCase(s1)) {
                     break;
                }
            }
        }

        String string = "the numbers as requested are : ";
        for (Integer x : numbers) {
            System.out.println(x);
        }
        scanner.close();

     }
 }
Community
  • 1
  • 1
AJefferiss
  • 1,628
  • 1
  • 13
  • 17
0

Here's a sample:

public static void main(String[] args) {
    int ARRAY_LENGTH = 5;
    int[] intArray = new int[ARRAY_LENGTH];
    int x = 0;

    try (Scanner reader = new Scanner(System.in)) {
        System.out.println("Enter " + ARRAY_LENGTH + " numbers [Something else exits]: ");
        while (x < ARRAY_LENGTH) {
            if (reader.hasNextInt()) {
                intArray[x++] = reader.nextInt();
            } else {
                System.out.println("Input not a number, exiting.");
                break;
            }
        }
    }

    //Print array
    System.out.print("Array has: ");
    for (int number : intArray) {
        System.out.print(number + " ");
    }
    System.out.println();
}

Here's another:

public static void main(String[] args) {
    ArrayList<Integer> array = new ArrayList();
    try (Scanner reader = new Scanner(System.in)) {
        System.out.println("Enter numbers [Something else exits]: ");
        while (reader.hasNextInt()) {
            array.add(reader.nextInt());
        }
    }

    //Print array
    System.out.print("Array has: ");
    for (int number : array) {
        System.out.print(number + " ");
    }
    System.out.println();
}
JorgeZ
  • 168
  • 8