This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting characters from a String

 
Adrian Burke
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew.
 
Nico Van Brandt
Ranch Hand
Posts: 66
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do something with Character.IsDigit() or Character.isLetter()?
I think you are smart enough to add some logic around it
 
Adrian Burke
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would do something like this (pseudocode):

 
Adrian Burke
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a solution offered by my collegue Ed Burke


Thanks Ed for your help..
 
Rob Spoor
Sheriff
Pie
Posts: 20393
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic