How to specify a range of values (from start to end) within an array list and from user input

Viewed 39
public static void main(String[] args) {
    Scanner inputReader = new Scanner(System.in);
    ArrayList<Integer> listOfNumbers = new ArrayList<>();
    int inputNumber;

    while (true) {
        inputNumber = Integer.valueOf(inputReader.nextLine());
        if (inputNumber <= -1) {
            break;
        } else {
            listOfNumbers.add(inputNumber);
        }
    }
    System.out.println("From where? " + inputNumber);
    System.out.println("To where? " + inputNumber);
    System.out.println(listOfNumbers);
}

I am trying to input random numbers [10, 20, 30, 40 and 50] to an array list, after I input -1 the while loop will stop running and the array list will stop collecting numbers.

After that, I want the user to input a first and an end value (index) assuming that the user knows how many numbers there are in an array list, to retrieve a range of values and after the loop is broken. Let's say the user inputs 10 and 30, the output should be [10, 20 and 30]

How can I do such a thing inside the main method and without calling other methods?

0 Answers
Related