Results 1 to 5 of 5
- 06-10-2008, 01:55 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 1
- Rep Power
- 0
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:
Java Code:String consonantString = (new Character(consonantChar)).toString();
Java Code:Char consonantChar = (new String(consonantString)).toChar();
Does anyone know how I can fix that?
Thanks in advance.
-
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.
- 06-10-2008, 05:32 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Fudarable try to explain something like this in simple words.
Java Code:String str = "Java"; char st = str.charAt(0); System.out.println(st);
- 06-14-2008, 02:22 AM #4
Member
- Join Date
- Jun 2008
- Posts
- 22
- Rep Power
- 0
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();
- 06-15-2008, 07:30 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Actually there is no such toChar()
And also, avoid funniest way all the time. Others can annoying, you too.
Similar Threads
-
How can i insert a char into a string
By Jamie in forum New To JavaReplies: 8Last Post: 02-17-2011, 09:59 PM -
Cannot convert from char to String error
By sondratheloser in forum New To JavaReplies: 1Last Post: 12-13-2007, 10:28 PM -
Assigning Threads on Multiprocessor?
By DrRufus in forum Advanced JavaReplies: 1Last Post: 08-08-2007, 11:55 PM -
Char to String in java
By trill in forum New To JavaReplies: 1Last Post: 08-01-2007, 02:42 PM -
Help with, String, Char
By lenny in forum New To JavaReplies: 1Last Post: 07-25-2007, 03:58 PM
Bookmarks