Author |
Message |
ProgrammingFun
![](http://compsci.ca/v3/uploads/user_avatars/11682880074bcb590d30b0a.png)
|
Posted: Thu Sep 30, 2010 5:46 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
jcollins1991
|
Posted: Fri Oct 01, 2010 8:04 am Post subject: 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... |
|
|
|
|
![](images/spacer.gif) |
Generic
|
Posted: Sun Oct 03, 2010 9:22 pm Post subject: RE:Extract chars from string |
|
|
code: |
String str = "str";
char[] arr = str.toLowerCase().toCharArray();
Arrays.sort(arr);
|
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. |
|
|
|
|
![](images/spacer.gif) |
DanShadow
![](http://compsci.ca/v3/uploads/user_avatars/12142970654c996e83e6997.png)
|
Posted: Sun Oct 03, 2010 9:36 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Mon Oct 04, 2010 3:05 am Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
ProgrammingFun
![](http://compsci.ca/v3/uploads/user_avatars/11682880074bcb590d30b0a.png)
|
Posted: Mon Oct 04, 2010 6:42 pm Post subject: 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):
Java: |
import java.util.*;
import java.awt.*;
public class SortThreeLetters
{
public static void main (String[] args )
{
Scanner letter = new Scanner (System. in);
String letterinput;
char l1, l2, l3;
System. out. print ("Please enter three lowercase letters: ");
letterinput = letter. next();
l1 = letterinput. charAt(1);
l2 = letterinput. charAt(2);
l3 = letterinput. charAt(3);
if (l1 >= 'a' && l2 >= 'a' && l3 >= 'a')
{
if (l1 < l2 )
{
if (l2 < l3 )
{
System. out. print (l1 + l2 + l3 );
}
else if (l3 < l2 )
{
System. out. print (l1 + l3 + l2 );
}
}
else if (l2 < l1 )
{
if (l1 < l3 )
{
System. out. print (l2 + l1 + l3 );
}
else if (l3 < l1 )
{
System. out. print (l2 + l3 + l1 );
}
}
else if (l3< l1 )
{
if (l1 < l2 )
{
System. out. print (l3 + l1 + l2 );
}
else if (l2 < l1 )
{
System. out. print (l3 + l2 + l1 );
}
}
}
else
{
System. out. print ("Error: Please follow instructions.");
}
}
}
|
Error wrote:
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);
|
|
|
|
|
![](images/spacer.gif) |
chrisbrown
![](http://compsci.ca/v3/uploads/user_avatars/18814724584bcbb8192aae8.png)
|
Posted: Mon Oct 04, 2010 6:59 pm Post subject: RE:Extract chars from string |
|
|
Java indexes start from 0, not 1. Also, you can use Character.isLowerCase(char) to test for lowercase letters. |
|
|
|
|
![](images/spacer.gif) |
ProgrammingFun
![](http://compsci.ca/v3/uploads/user_avatars/11682880074bcb590d30b0a.png)
|
Posted: Mon Oct 04, 2010 7:42 pm Post subject: Re: RE:Extract chars from string |
|
|
chrisbrown @ Mon Oct 04, 2010 6:59 pm wrote: Java indexes start from 0, not 1. Also, you can use Character.isLowerCase(char) to test for lowercase letters.
Thanks for the help! |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Tue Oct 05, 2010 4:16 am Post subject: 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). |
|
|
|
|
![](images/spacer.gif) |
TheGuardian001
|
Posted: Tue Oct 05, 2010 5:13 am Post subject: Re: RE:Extract chars from string |
|
|
DemonWasp @ Tue Oct 05, 2010 4:16 am wrote: 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.) |
|
|
|
|
![](images/spacer.gif) |
|