aspose file tools*
The moose likes Beginning Java and the fly likes Extracting characters from a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extracting characters from a String" Watch "Extracting characters from a String" New topic
Author

Extracting characters from a String

Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11
Hi,

I would like to enhance this piece of code to do the following:
- add the characters to a single element in an array.
- add the digits to the next element in the same array.




Result required is : [ABC, 123, DEF]

Can anyone help?

Thank you in advance
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

Hi Adrian. Welcome to The Ranch!

Your code is easier to read if you UseCodeTags. I've added them for you this time.
Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11
Thanks Matthew.
Nico Van Brandt
Ranch Hand

Joined: Mar 31, 2011
Posts: 66

You could do something with Character.IsDigit() or Character.isLetter()?
I think you are smart enough to add some logic around it


Oracle Java SE6 Certified Programmer
Oracle Java EE5 Certified Web Component Developer
Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11
Thanks Nico for the advice.



Result:
1st element: nullABCDEF
2nd element: null123
3rd element: nullABCDEF

Now, I am having a different problem. All Letter characters are been added to the first element in the array?

Regards,
Adrian.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I would do something like this (pseudocode):



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11
Appreciate the pseudocode. Thank you.





How can I get the previous index and compare it to the current index going through the iterator? If I could determine the previous character and current character, I could then pass them as arguments to the addCharacter()
Eg : addCharacter(char previousCharacter char currentCharacter)






Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11
Here is a solution offered by my collegue Ed Burke


Thanks Ed for your help..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

I see two issues with that:
1) if the String is empty it will throw an exception (StringIndexOutOfBoundsException if I recall correctly)
2) the String concatenation is not that efficient. Switching to StringBuilder would be better.

The only differences are:
- return strings without doing anything if the String is empty.
- nextStringToInsert becomes a StringBuilder, with the String size as capacity, and initial contents the first character.
- don't add nextStringToInsert to the List but nextStringToInsert converted into a String (toString()).
- appending is done by using the append method instead of +=
- re-initializing is done by removing the current contents (delete(...)), then adding the next character.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extracting characters from a String