Because I'm just a beginner, I always wonder if there are better ways to do things than how I do them. I'm just teaching myself. Today, I decided to write a program that collected integers from the user and then sorted them. It works fine. In trying to "tighten up" the program, I am looking at a few lines of my code that collects numbers from the user:
Scanner sc = new Scanner(System.in);
String [] userEnteredNumbers = new String[MAX_SIZE];
int [] convertedNumbers = new int[MAX_SIZE];
int index = 0;
System.out.println("Enter some integers to sort:");
userEnteredNumbers = sc.nextLine().split("\\s*\\s");
for (String someNumber: userEnteredNumbers)
convertedNumbers[index++] = Integer.parseInt(someNumber);
------------
This seems like entirely too many lines of code that should be needed just to get user entered data and place it in some sort of collection device (I used arrays, but anything is fine really). So I want to ask developers more experienced than myself: How would you do it?
(By the way, the reason I posted the code I have is because I want to make it clear I'm not using this group for homework. I'm not in class, but if I were, I have something that works. But what I have isn't elegant or pretty, and I would like to know how to write better code.)