Java string

Display the character of a word in ascending order

Page 1 of 1

12 Replies - 2308 Views - Last Post: 22 June 2009 - 03:47 PM Rate Topic: -----

#1 dilshu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-June 09

Java string

Post icon  Posted 21 June 2009 - 04:47 AM

How to display the alphabet of a word in ascending order.
Is This A Good Question/Topic? 0
  • +

Replies To: Java string

#2 dilshu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-June 09

Re: Java string

Post icon  Posted 21 June 2009 - 04:49 AM

write a program that will display all the words from a given string,the word should start and end wit a character 'a'.
Was This Post Helpful? 0
  • +
  • -

#3 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: Java string

Posted 21 June 2009 - 04:59 AM

Ask in the JAVA forum.
Was This Post Helpful? 0
  • +
  • -

#4 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: Java string

Posted 21 June 2009 - 04:59 AM

Ask in the JAVA forum.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Java string

Posted 21 June 2009 - 05:50 AM

Topics merged & moved to Java forum :)
Was This Post Helpful? 0
  • +
  • -

#6 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Java string

Posted 21 June 2009 - 07:50 AM

well how about breaking it down using charAt( int index ); for each String and casting that to its integer form. for example

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

Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Java string

Posted 21 June 2009 - 05:44 PM

You can directly compare characters without converting them to ints or doubles b/c the char datatype is representative of base 256. So for example, the following is a legitimate boolean expression in Java.

'a' > 'b'


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java string

Posted 21 June 2009 - 06:36 PM

String.toCharArray()
is your best friend
Was This Post Helpful? 0
  • +
  • -

#9 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Java string

Posted 21 June 2009 - 07:37 PM

View Postmacosxnerd101, on 21 Jun, 2009 - 04:44 PM, said:

You can directly compare characters without converting them to ints or doubles b/c the char datatype is representative of base 256. So for example, the following is a legitimate boolean expression in Java.

'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
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

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!
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java string

Posted 22 June 2009 - 02:45 PM

char[] digit = string.toCharArray();
Arrays.sort(digit);
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Java string

Posted 22 June 2009 - 02:47 PM

ugh, owned by pbl again >_<

No matter what you come up with, he shall always come up with same thing that is easier and shorter!!
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java string

Posted 22 June 2009 - 03:47 PM

That's because I am lazy
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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1