My program is that the ArrayList stores the entered number, and when the user enters the number -1, the program will stop end print out all the entered number in ascending order. My program runs, but let the number enters, it doesn't print the numbers out. it seems like if statement doesn't working. how to call the ArrayList in if statement to work right?
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class testArrayList {
public static void main(String[] args) {
Scanner inputs = new Scanner(System.in);
ArrayList <Integer> nums = new ArrayList<Integer>();
System.out.println("Enter a number(-1 to end): ");
while(inputs.hasNextInt()) {
nums.add(inputs.nextInt());
}
// it looks like it runs until here.
for (Integer n : nums) {
if (n == -1) {
Collections.sort (nums);
System.out.println("Here is the list of numbers : ");
System.out.println(n);
}
}
}
}