• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String manipulation.

 
Ted Scofield
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

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?

Thanks.

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ted Scofield
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hm .. in that case you could create a class that holds the value as double and sets a flag whether or not this value came in as double or int. make this class comparable and you should be good to go
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't you forgo making these into numbers at all and sort the numbers by padding the strings in front and back lining up the whole numbers based on there column values?

What i mean is line them up, sort them, and then trim them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic