Top.Mail.Ru
June 17th, 2006 - Java developers — LiveJournal
? ?

Java developers

June 17th, 2006
 

08:22 pm - tmagr - Beginning user-input question

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.)

 

08:52 pm - curious_tiger - arrays to linked lists

This seems like a Java deficiency to me. Let's say I have a standard vanilla array like:
int[] myNums = {1,1,2,3,5,8,13}.
And all I want to do is turn it into a linked list. I don't understand why the LinkedList class doesn't just have a constructor that looks like:
LinkedList(arrayName).

I mean, I can use the add method to add the array items one-by-one into the LinkedList, but I just don't see why this is necessary, from a design perspective...
Powered by LiveJournal.com