This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extracting characters from a String" Watch "Extracting characters from a String" New topic

Extracting characters from a String

Adrian Burke

Joined: Apr 12, 2011
Posts: 11

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

Joined: Apr 06, 2010
Posts: 4544

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

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

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

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?

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

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

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

Thanks Ed for your help..
Rob Spoor

Joined: Oct 27, 2005
Posts: 20279

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Extracting characters from a String
It's not a secret anymore!