- Unlike C,
char[]
is not aString
, and vice versa. You need to specify the class:
Str2 = String.copyValueOf( Str1 );
Array of character defined inside a quotation marks?
Question
I got this example code from here:
http://www.tutorialspoint.com/java/java_string_copyvalueof.htm
public class Test{
public static void main(String args[]){
char[] Str1 = "This is really not immutable!!";
String Str2;
Str2 = copyValueOf( Str1 );
System.out.println("Returned String " + Str2);
Str2 = copyValueOf( Str1, 5, 10 );
System.out.println("Returned String " + Str2);
}
}
This code is not working for me.
- array of character is defined as a String.
- copyValueOf is not recognisable!
Now I change it to this:
char[] Str1 = {'t','o','o','k'};
String Str2;
Str2 = copyValueOf( Str1 );
System.out.println("Returned String " + Str2);
Str2 = copyValueOf( Str1, 5, 10 );
System.out.println("Returned String " + Str2);
Still copyValueOf is not working? I have checked this method and it exist on documentation!
Pas de solution correcte
Autres conseils
You might be missing a static import from your code. Add this to the top of your file:
import static java.lang.String.copyValueOf;
Alternately, you could (and perhaps should) specify the class explicitly. Since copyValueOf
is a static member of String
that would look like this:
String.copyValueOf(Str1);
Here are the problems:
- You cannot assign a char array by a string. They are different in Java. You can see Post: Converting String to Character array in Java for an example of that.
- You should use
String.copyValueOf()
as it is a static class method ofString
.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow