I had posted a question earlier attempting to find an error in my code, and the main problem was that I had used a for each loop incorrectly. I’m new to Java and was wondering why what I had attempted was incorrect and if there would be any possible way of doing the task with a for each loop.
public Dids(String thing)
{
letters= new char[thing.length()];
for(char i: letters){
letters[i] = thing.charAt(i);
}
}
My thought was that since I had declared the size of the array before the loops, that I would be able to iterate through it.
With foreach constructs, there is no counter for the current index, just the value at the index. The variable ‘i’ is the value at the current point in the array.
You can do this with an ordinary for loop
Or more concisely,
See String.toCharArray()