0

I was working on this problem using arrays and array lists, and then I want to sort the array list to that the numbers the user enters are in ascending over. When I try to run this, I get two error codes, 1: the period on the list.add(num1); is not recognized, and then the other error code is that the Arrays.sort(list); is not recognized, I honestly have no clue what to do, I searched the web all over and could not find anything, this is the sole reason I made this account xD. Please help!!!

import java.util.Scanner;
import java.util.*;
import java.util.Arrays;

public class randomArrayList{
    public static void main(String[] args){
        int n = 5;
        ArrayList<Integer> list = new ArrayList<Integer>(n);
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter 5 numbers: ");
        int num1 = input.nextInt();
        list.add(num1);
        int num2 = input.nextInt();
        list.add(num2);
        int num3 = input.nextInt();
        list.add(num3);
        int num4 = input.nextInt();
        list.add(num5);
        int num5 = input.nextInt();
        list.add(num5);
        Arrays.sort(list);
        System.out.println(list);
    }
}
9
  • 1
    Please beware that Scanner#nextInt won't read the dangling new line left in the input buffer Commented Apr 7, 2020 at 23:51
  • 1
    can not add list.add(num5); before you have declared the variable. I think you want to add num4 Commented Apr 7, 2020 at 23:52
  • 1
    Arrays.sort(list); as the name implies is for sorting Arrays. Commented Apr 7, 2020 at 23:52
  • Yeah, use Collections.sort(list) instead. Commented Apr 7, 2020 at 23:53
  • So why does Collections work instead of Arrays? because it's only one array list instead of many? Commented Apr 7, 2020 at 23:57

1 Answer 1

1

This is what you want

Note

can not add list.add(num5); before you have declared the variable. I think you want to add num4

and

Arrays.sort(list); as the name implies is for sorting Arrays.

use Collections.sort(list) instead

int n = 5;
ArrayList<Integer> list = new ArrayList<Integer>(n);
Scanner input = new Scanner(System.in);

System.out.println("Please enter 5 numbers: ");
int num1 = input.nextInt();
list.add(num1);
int num2 = input.nextInt();
list.add(num2);
int num3 = input.nextInt();
list.add(num3);
int num4 = input.nextInt();
list.add(num4);
int num5 = input.nextInt();
list.add(num5);
Collections.sort(list);
System.out.println(list);

Although a cleaner way would to be use a loop

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

6 Comments

Thank you!!! I think I understand it now, I'm just lost on why Collections works but Arrays doesn't.
Arrays.sort works on Arrays, Collections.sort works on Collections.
I'm sorry for asking, but could you please explain to me the difference between the two?
What don't you understand? They are two different pieces on code working on different type of Objects.
I'm sorry, I just was misunderstanding that I could use the Arrays method to help me with this, but I get what it is now :).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.