Reputation: 1
I am trying to create tolowercase method with using ascii code. I found this code:
public void toLowerCase(){
for (int i = 0; i<arraySize; i++){
char aChar = myarray[i].charAt(0);
if (65 <= aChar && aChar<=90){
aChar = (char)( (aChar + 32) );
}
System.out.print(aChar);
}
}
But myarray is generic array so, charAt gives an error
The method charAt(int) is undefined for the type T
How can i fix this? I try to create a new array of String and convert generic array to new array but it does not work. How can i convert generic array to string array or how can i create tolowercase method with using ascii code in generic array?
Upvotes: 0
Views: 758
Reputation: 533690
Unless it's a class or interface which has a charAt
method you have nothing to call. What you can do is make your generic type T extends CharSequence
and you will be able to use charAt
as the CharSequence
interface has this method.
Classes which implement CharSequence include String, StringBuffer and StringBuilder.
How can i convert generic array to string array or how can i create tolowercase method with using ascii code in generic array?
You can do this
T[] myarray = {"to", "be", "or", "not", "TO", "be"};
String[] strs = (String[]) myarray;
though it would be much simpler to do
String[] myarray = {"to", "be", "or", "not", "TO", "be"};
Upvotes: 2
Reputation: 18933
This is what should do your job:
char myarray[] = {'A','F'};
int arraySize = 2;
for (int i = 0; i<arraySize; i++){
char aChar = myarray[i];
if (65 <= aChar && aChar<=90){
aChar = (char)( (aChar + 32) );
}
System.out.print(aChar);
}
}
Why do you need the charAt()
?
You are using a character array. If you use a string array then you can try to use that.
For a String array you can do something like this(if you don't want to use toLowerCase()
):
public static void toLowerCaseString(){
String myStringArray[] = {"Something","Nothing"};
for (int i = 0; i<myStringArray.length; i++){
for (int j = 0; j< myStringArray[i].length(); j++){
char aChar = myStringArray[i].charAt(j);
if (65 <= aChar && aChar<=90){
aChar = (char)( (aChar + 32) );
}
System.out.print(aChar);
}
}
}
Upvotes: 0