'trouble printing a range of numbers in an array list with own inputs and indices - 3.8 MOOC JAVA

I have an exercise problem from MOOC.fi that I am stuck on! We are learning about arrays and lists in Java, and here is the text for the problem:

"The exercise template contains a base that reads numbers from the user and adds them to a list. Reading is stopped once the user enters the number -1.

Expand the program to ask for a start and end indices once it has finished asking for numbers. After this the program shall prints all the numbers in the list that fall in the specified range (between the indices given by the user, inclusive). You may assume that the user gives indices that match some numbers in the list." The instructions also emphasize using lists and indices.

Here is my code:

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

public class OnlyTheseNumbers {

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

        ArrayList<Integer> numbers = new ArrayList<>();
        while (true) {
            int number = Integer.valueOf(scanner.nextLine());
            if (number == -1) {
                break;
            }

            numbers.add(number);
        }
        
        System.out.println("From where? ");
        int first = Integer.valueOf(scanner.nextLine()); //first index input  to 
        System.out.println("To where? ");
        int last = Integer.valueOf(scanner.nextLine()); // last index input in a list
        
        
        for (int i = numbers.get(first); i <= numbers.get(last); i++) {
            System.out.println(numbers);
        }         
        
        /*int i = numbers.get(first);
        while (i <= numbers.get(last)) {
            System.out.println(arr[numbers]);
            i++;
        }*/

    }
}

Result - If I use input numbers: 3 2 1 4 7

From where? 2

To where? 4

[3, 2, 1, 4, 7]

[3, 2, 1, 4, 7]

[3, 2, 1, 4, 7]

[3, 2, 1, 4, 7]

[3, 2, 1, 4, 7]

[3, 2, 1, 4, 7]

[3, 2, 1, 4, 7]

It works up until it needs to print the range of inputs, between the first and last indices, I have tried using i instead of numbers in the System.out.println(numbers);, but that prints the first input number to the last input number. I've tried both a while loop and a for loop, but am stuck.

This is my first post, I apologize if it is bad formatting or too much text!



Solution 1:[1]

if you use java 8 and above:

System.out.println(IntStream.rangeClosed(first, last).mapToObj(number::get).collect(Collectors.toList()));

if you dont want to use a stream:

for (int i = first; i <= last; i++) {
        System.out.println(numbers.get(i));
    } 

Solution 2:[2]

just replace your for loop with this:

for (int i = first; i < last; i++) {
      System.out.println(numbers.get(i));
}

Solution 3:[3]

Try to understand the code that you have written. Your for loop indicates that it initializes from the element 1 that is stored in index2 and will stop at element 7 that is stored in index4
now that means

for (int i = numbers.get(first); i <=numbers.get(last); i++) { System.out.println(numbers); } this will indirectly say

for( int i =1;i<=7;i++){
System.out.println(numbers);}

that is why your code is printing the array 7 times.

        for (int i =first; i <= last; i++) {
        System.out.println(num.get(i));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 MehranB
Solution 3