Originally Posted by harold
are not doing what you think. Replace the first with
ch = console.next().charAt(0);
and delete the second one.
ch = str.charAt(i);
Now to be able to debug your own programs, you need a way to check that the program is doing what you think it is doing. There are two ways to go:
- use a debugger to step through the program and show you the values of the variables and what functions are being executed, or
- put in print statements. For example, if you just printed out the value of ch within the loop, you would see it does not have the values you think. And if you printed out str, you'd see it does have the values you want.
Debuggers are good if you have one and know how to use it. Otherwise, start putting in print statements when you are puzzled by the behavior of a program.
And by the way, the need to be skilled in debugging programs does not go away with experience. The programs just get more complicated, the mistakes more subtle, and the need to be skilled in debugging increases.