i tried to use System.in.read() to get the age of the user. My problem is if i write "24" it gives me the output "you are 50 years old". I googled and saw that it has something to do with the encoding. So 24 is mapped to 50.
Someone recommended to cast integer into char. So it should work. But this time when I put 24, I get the answer "you are 2 years old".
What I want is, that I get the answer "you are 24 years old". How do I do this?
My code below: First Try
public static void main(String[] args) throws IOException {
System.out.println("Hallo " + args[0]);
System.out.println("Wie alt bist du?");
int alter = System.in.read();
System.out.println("Du bist " + alter + " Jahre alt.");
}
Output: "You are 50 years old."
Second Try:
public static void main(String[] args) throws IOException {
System.out.println("Hallo " + args[0]);
System.out.println("Wie alt bist du?");
char alter = (char) System.in.read();
System.out.println("Du bist " + alter + " Jahre alt.");
}
Output: "You are 2 years old."
Small Note: "Alter" means age in german.
I hope someone can help me and understands the question. If I can improve something please tell me. Would be appreciated.