File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String manipulation. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String manipulation." Watch "String manipulation." New topic
Author

String manipulation.

Ted Scofield
Greenhorn

Joined: Apr 04, 2009
Posts: 26
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

Joined: Feb 23, 2009
Posts: 1183
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


JDBCSupport - An easy to use, light-weight JDBC framework -
Ted Scofield
Greenhorn

Joined: Apr 04, 2009
Posts: 26
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

Joined: Feb 23, 2009
Posts: 1183
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

Joined: Oct 27, 2005
Posts: 19684
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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.
 
 
subject: String manipulation.