0

Java novice here. So, I was learning Java from this book, Java and Algorithmic Thinking for the Complete Beginner, by Aristides S. Bouras and was stuck at learning the syntax for user input. Here's the original code snippet from the book

java.io.BufferedReader cin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));

String name;
int age;

name = cin.readLine();
age = Integer.parseInt(cin.readLine());
System.out.print("Wow, you are already" + age + "years old," + name + "!");

I tried using the exact code from above in IntelliJ, but I got the following error:

enter image description here

I was thinking to "walk around" this problem with other types of input methods from this article using the scanner method, but any insight into the error would be highly appreciated. And also, I believe that including the "java.io." for three times in a row is unnecessary for the first line of code right? Please humble my questioning if there's a reason to it. Thank you all!

Naman
  • 27,789
  • 26
  • 218
  • 353
ken_you_not
  • 93
  • 1
  • 1
  • 11
  • see https://stackoverflow.com/questions/14897448/use-of-in-import-statement-in-java – Scary Wombat Jan 22 '20 at 00:49
  • 4
    You must use the fully qualified class name if you do not `import` the class (or if you need to disambiguate between two classes with the same name in different packages); as for the exceptions, there should have been an early chapter on the `try-catch` statement. You need one of those, or to add a `throws IOException` to your `main` signature. – Elliott Frisch Jan 22 '20 at 00:49
  • @ScaryWombat. Thank you for the swift comment! I'm sorry, but I should've mentioned this earlier; that I've already done "import java.io.BufferedReader;" and "import java.io.InputStreamReader;" before the code and adding the asterisk wouldn't work as well as IntelliJ warned me for "; expected". I tried restarting the app, but its still there. What have I done wrong? – ken_you_not Jan 22 '20 at 01:06
  • You still need `;` with star imports (e.g. `import java.io.*;`). – Slaw Jan 22 '20 at 01:08
  • @ElliottFrisch. Thank you for the fast response Elliott! I believe your comment and Wombat's are somewhat tied together though I'm not sure yet, but when you say using "fully qualified class name", you're implying that if I don't import the classes (in my case, BufferedReader and InputStreamReader), I would have to do "java.io." right? As for the try-catch, I will look on it asap and try to see there's anything I could do to fix it. Let me know your thoughts. – ken_you_not Jan 22 '20 at 01:09
  • @Slaw. Thank you for the reminder, but problem still persisted on my end. Any thoughts? – ken_you_not Jan 22 '20 at 01:17

1 Answers1

2

There is no difference between using BufferedReader and java.io.BufferedReader, other than how the human reads the code. That is, the resulting code won't be faster/slower, or correct/incorrect, etc. using one vs. the other. Note that if you use BufferedReader in your code, you will need to include an import statement so that the compiler knows where to find BufferedReader, like this: import java.io.BufferedReader;.

You're running into something else though by calling cin.readLine(). Here's an example showing the most minimal version of what you're doing (I omitted the import statements, but you could also include import java.io.*;). The compiler is showing you that there's a problem with readLine() because that method is defined to throw an exception and your code isn't handling it. In IntelliJ, I see this error message: unreported exception java.io.IOException; must be caught or declared to be thrown.

public static void main(String[] args) {
    BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    cin.readLine();
}

Here's one way to fix your code so that it handles exceptions thrown by readLine():

public static void main(String[] args) {
    BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
    try {
        cin.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Kaan
  • 5,434
  • 3
  • 19
  • 41
  • Hello @kaan, thank you for the well thought out answer! I can understand everything you mentioned. In fact, when I tried including the asterisk to ```java.io.BufferedReader```, IntelliJ would give me an error saying a semicolon is expected, but its more like it was not expecting the asterisk to be there since we are already at a class and it has nothing to import anymore right? Also, I like the idea of exception handling here since that's what I used to do in C++, but is this the norm most Java programmers would use (sorry if I assumed)? Looking forward to your reply! – ken_you_not Jan 22 '20 at 08:18
  • 1
    Please see the good article describing the exception handling practices in Java: https://www.baeldung.com/java-exceptions. BTW if you put the cursor on the readLine() method underlined by IntelliJ IDEA, and press "Alt+Enter", two possible fixes will be suggested: 1) adding exception to method signature and 2) surrounding the method invocation with try-catch block. – Olga Klisho Jan 22 '20 at 09:07