Open Marek707 opened 2 years ago
To debug this, I removed the (char)
cast from ch
and added System.out.println("Input: " + ch);
after the ch = System.in.read();
line to see exactly what was being input after entering a capital letter then pressing Enter. Unsurprisingly, the capital letter is input and so is an ASCII carriage return (which has value 13), which causes the loop to repeat "Enter a letter" twice from the second attempt onwards.
This can be fixed by shuffling around where you call System.out.println("Enter a letter");
.
The following code works on my system:
char ch;
System.out.println("Enter a letter");
for(;;) {
ch = (char) System.in.read();
if (ch > 64 && ch < 91) {
ch += 32;
System.out.println(ch);
System.out.println("Enter a letter");
}
}
Let me know if you have any questions :)
It works fine. Thank you for your explanation!
The program is to change a capital letter into a lowercase letter, and this is what it does pretty well. However, why is the phrase "Enter a letter" repeated twice at the second attempt and all following attempts? How can I correct it?
package com.mycompany.mavenproject2;
public class test1 { public static void main (String args[])throws java.io.IOException {
}