Java stringDisplay the character of a word in ascending order
Page 1 of 1
12 Replies - 1727 Views - Last Post: 22 June 2009 - 03:47 PM
#1
Java string
Posted 21 June 2009 - 04:47 AM
Replies To: Java string
#2
Re: Java string
Posted 21 June 2009 - 04:49 AM
#6
Re: Java string
Posted 21 June 2009 - 07:50 AM
class Test
{
public static void main(String[] args)
{
String p = "DCBA";
char v_c;
int v;
for (int i = 0; i < p.length(); i++)
{
v_c = p.charAt(i);
v = (int)v_c;
System.out.println(v_c + " = " + v + " if we take the int value of it");
}
}
}
that way you can use integers to work out alphabetical order ? eg the char 'A' may have an integer value of 42 while 'D' may be 45 in integer form.
Note the output and gl with the program
This post has been edited by bbq: 21 June 2009 - 07:57 AM
#7
Re: Java string
Posted 21 June 2009 - 05:44 PM
'a' > 'b'
#8
Re: Java string
Posted 21 June 2009 - 06:36 PM
is your best friend
#9
Re: Java string
Posted 21 June 2009 - 07:37 PM
macosxnerd101, on 21 Jun, 2009 - 04:44 PM, said:
'a' > 'b'
Yep sure can however using their int values exemplifies the fact that they do have integer values - saying 'a'>'b' to a new comer could be a bit ambiguous. Also pbl's suggestion would work niceley
#10
Re: Java string
Posted 21 June 2009 - 09:59 PM
String name = cba;
char temp = ' ';
char[] word = name.toCharArray();
for(int i = 0; i<word.length;i++)
for(int x = 0; x<word.length-1;x++)
if(word[x] > word[x+1])
{
temp = word[x];
word[x] = word[x+1];
word[x] = temp;
}
Thats your classic sort. as pbl said, toCharArray is your best friend, to make it descending switch the condition to '<'
Happy Coding!
#11
Re: Java string
Posted 22 June 2009 - 02:45 PM
Arrays.sort(digit);
#12
Re: Java string
Posted 22 June 2009 - 02:47 PM
No matter what you come up with, he shall always come up with same thing that is easier and shorter!!
#13
Re: Java string
Posted 22 June 2009 - 03:47 PM
The lazier you are the shortest code you produce
Why re-inventing the wheel when tools are there ?
This post has been edited by pbl: 22 June 2009 - 06:12 PM
|
|

New Topic/Question
Reply




MultiQuote







|