I'm having some doubts/problems on how to manipulate a String containing numbers. My task is to sort the numbers given from a standard input
and then print them. All the "types" should remain the same, meaning a String representation of an int must remain an int (in other words, "1" must
not become "1.0").
Here is an example of a correct input:
where "+" means ascending order. The syntax of an expression is defined as: [ORDER SIGN]+[SPACE] followed by [NUMBER]+[,]+[SPACE]
I've come up with a solution I'm not particulary happy with. This is what I've done:
I should use Arrays.sort() method to sort the array. However, this makes sense only if the values
in the array are a String representation of a number. Thus I should check the String array:
If this turns out to be OK, the following method is invoked:
I'm looking for comments, improvements or a bit more elegant approach. Could you guys help me?
Sebastian Janisch wrote:If the user input is 5.0, is it okay to display it as 5 ??
In that case just convert everyting to double, sort it and when displaying it as text again check if the number is a whole number and if that is the case strip the .0 to convert 5.0 back to 5
No. Unfortunately, I must use every String representation as is, meaning "5.0" can be displayed only as "5.0" or as a double value 5.0.