Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-10-2008, 01:55 AM
Member
 
Join Date: Jun 2008
Posts: 1
coffeebean is on a distinguished road
Assigning a string value to a char
I've been trying to assign a string (consonantString) to a char (consonantChar). I found and tried using the following code:

Code:
String consonantString = (new Character(consonantChar)).toString();
Since the code assigns a char to a string, I tried changing it so it assigned a string to a char and I ended up with:

Code:
Char consonantChar = (new String(consonantString)).toChar();
However, the to.Char() caused an error and the program said I should use toCharArray(); instead. When I tried that, it didn't work either.

Does anyone know how I can fix that?

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-10-2008, 02:49 AM
Senior Member
 
Join Date: Jun 2008
Posts: 468
Fubarable is on a distinguished road
One of your problems here is char is just that, a single isolated character. So if you want to convert your String to a "char" the obvious question is: which char of the string do you mean? Fortunately you can give the compiler the answer by using the String method: charAt(int index) where index is the position of the char that you want to extract from the String.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-10-2008, 05:32 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,596
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Fudarable try to explain something like this in simple words.

Code:
String str = "Java"; char st = str.charAt(0); System.out.println(st);
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-14-2008, 02:22 AM
Member
 
Join Date: Jun 2008
Posts: 22
ferranb is on a distinguished road
Any way, just for teach, the rigth use of toChar() is like follows:

String s = "hello";
char[] c = s.toCharArray();

Or funniest way:

char[] c = "hello".toCharArray();
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-15-2008, 07:30 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,596
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Actually there is no such toChar()

And also, avoid funniest way all the time. Others can annoying, you too.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot convert from char to String error sondratheloser New To Java 1 12-13-2007 10:28 PM
Assigning Threads on Multiprocessor? DrRufus Advanced Java 1 08-08-2007 11:55 PM
Char to String in java trill New To Java 1 08-01-2007 02:42 PM
Help with, String, Char lenny New To Java 1 07-25-2007 03:58 PM
How can i insert a char into a string Jamie New To Java 2 05-20-2007 10:01 PM


All times are GMT +3. The time now is 10:56 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org