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

1
  • @DarshanDatta Look at my solution below, a short and simple answer is probably what you are looking for. Commented Sep 14, 2015 at 14:40

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Remarks: You can always change it to a while loop with a terminating condition, for example prompt a (y/n) for continuation.
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 .

Comments

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();
}

Comments

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();

     }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.