Why Use String Input Instead of Int for Numbers in Java?

java

I'm a beginner in Java and I have a question from the "Head First Java" book. In the book, the author creates a method that takes the number as String and then convert it into int. Why do I need to take the input as a string and convert it rather than directly take it as an int. Below is he screenshot that I'm talking about.

enter image description here

Best Answer

Simply because the user's guess is most probably entered through a mechanism which only supports entering chars, or strings, but no other types. For example, System.in.read() most GUIs have a text-input control, but not a numeric input control. So, the user enters text in the control, and then the program must convert the text to a number.

Related Topic