Jump to content
Sign in to follow this  
Alduin768

Java Error: "char cannot be dereferenced"

Recommended Posts

Why does this give an error and how do I fix it? :confused:

[CODE]
public static String capitalizeString(String stringTo)
{
String newString = "";

//Converts the string to a CharArray
char stringarray[] = stringTo.toCharArray();

//Sets the first letter of the CharArray to UpperCase
//This line gives an error
stringarray[0] = stringarray[0].toUpperCase();

//Converts the CharArray back to a String
for (int i = 0; i < stringarray.length; i++)
{
newString = newString + stringarray[i];
}

//Returns the newly capitalized string
return newString;
}
[/CODE]

Share this post


Link to post
Share on other sites
Why are you doing it like this :p

You could easily do this:
[CODE]

package org.arsenal;

public class Test {

public static void main(String[] args) {
System.out.println(capitalizeString("emily"));
}

public static String capitalizeString(String value) {
return value.substring(0, 1).toUpperCase() + value.substring(1);
}
}

[/CoDE]

If you need something explained, just ask.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×