Results 1 to 5 of 5
  1. #1
    coffeebean is offline Member
    Join Date
    Jun 2008
    Posts
    1
    Rep Power
    0

    Default 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();
    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:

    Java 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Fudarable try to explain something like this in simple words.

    Java Code:
            String str = "Java";
            char st = str.charAt(0);
            System.out.println(st);

  4. #4
    ferranb is offline Member
    Join Date
    Jun 2008
    Posts
    22
    Rep Power
    0

    Default

    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();

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. How can i insert a char into a string
    By Jamie in forum New To Java
    Replies: 8
    Last Post: 02-17-2011, 08:59 PM
  2. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 09:28 PM
  3. Assigning Threads on Multiprocessor?
    By DrRufus in forum Advanced Java
    Replies: 1
    Last Post: 08-08-2007, 10:55 PM
  4. Char to String in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 01:42 PM
  5. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •