So, I was always a novice programmer but recently decided to learn Java. I was just messing around with a small constructor that was to take in a string and then write it backwards (my method of doing this was in no way supposed to be efficient, I was simply using different tools available to me in order to become accustomed.) My error came when I was trying to add a char in an array to a string. This is the code:
public class dids {
char letters[];
public dids(String thing)
{
letters= new char[thing.length()];
for(char x:letters){
letters[x] = thing.charAt(x);
}
for(int i=thing.length();i>0;i--){
String retval += letters[i];
}
}
}
The error is saying I cannot add a char to a string. A type mismatch.
you want to use String.toCharArray to populate your array like so:
letters = thing.toCharArray();The below code reverse a String.