
-----------------------------------
ProgrammingFun
Thu Sep 30, 2010 5:46 pm

Extract chars from string
-----------------------------------
How can we extract chars from a string in Java?
I need to input a string of three letters and sort in alphabetical order if they are all lowercase...
...how would I go about doing so?

I am not asking for a program, just the concept.

-----------------------------------
jcollins1991
Fri Oct 01, 2010 8:04 am

Re: Extract chars from string
-----------------------------------
You could use something like string.split with "" input to split it into an array of single characters then sort however you like... Or if java lets you you might be able to just access the individual characters something like str[n] and change them directly...

-----------------------------------
Generic
Sun Oct 03, 2010 9:22 pm

RE:Extract chars from string
-----------------------------------
[code]
String str = "str";
char[] arr = str.toLowerCase().toCharArray();
Arrays.sort(arr);
[/code]
Would sort all the characters. If you want to output it in ordered form make a new String using that char array and output it.

-----------------------------------
DanShadow
Sun Oct 03, 2010 9:36 pm

RE:Extract chars from string
-----------------------------------
Tons of ways to do it, one being substring.
You could loop through the length of the string, check the substring(x,1) of a string, character by character and sort it on each loop iteration.

-----------------------------------
DemonWasp
Mon Oct 04, 2010 3:05 am

RE:Extract chars from string
-----------------------------------
If you want a mutable String, use StringBuilder. It's like String in many ways, but Strings are immutable. StringBuilder will let you determine the character at a given index easily (though so will String...use charAt()) and let you assign characters.

-----------------------------------
ProgrammingFun
Mon Oct 04, 2010 6:42 pm

Re: Extract chars from string
-----------------------------------
Thanks for the suggestions...but I had to do it using if-else structure...
So, posted below is my code, however, I still get a compile error on it (don't know why):


   import java.util.*;
   import java.awt.*;

   public class SortThreeLetters
   {
   
      public static void main (String


Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
	at java.lang.String.charAt(String.java:686)
	at SortThreeLetters.main(SortThreeLetters.java:18)

highlights:
         l3 = letterinput.charAt(3);


-----------------------------------
chrisbrown
Mon Oct 04, 2010 6:59 pm

RE:Extract chars from string
-----------------------------------
Java indexes start from 0, not 1. Also, you can use Character.isLowerCase(char) to test for lowercase letters.

-----------------------------------
ProgrammingFun
Mon Oct 04, 2010 7:42 pm

Re: RE:Extract chars from string
-----------------------------------
Java indexes start from 0, not 1. Also, you can use Character.isLowerCase(char) to test for lowercase letters.
Thanks for the help!

-----------------------------------
DemonWasp
Tue Oct 05, 2010 4:16 am

RE:Extract chars from string
-----------------------------------
It's worth noting that "if ( 13 < 11 )" will always be false. The compiler notes this and will, in fact, compile that check out entirely (it will never be run, with a reasonable compiler).

-----------------------------------
TheGuardian001
Tue Oct 05, 2010 5:13 am

Re: RE:Extract chars from string
-----------------------------------
It's worth noting that "if ( 13 < 11 )" will always be false. The compiler notes this and will, in fact, compile that check out entirely (it will never be run, with a reasonable compiler).
It's not actually 11/12/13, its L1/L2/L3. Gotta love fonts (don't worry, it got me for a minute too.)
