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?
Or just use BigDecimal. This keeps the precision exactly as you give it, and can be constructed from a String. When creating using "6", the BigDecimal's toString() will return "6", and when creating using "5.0", "5.0" is returned.