0
\$\begingroup\$

I'm working through the Head First Java book, and one piece of code it makes me write looks like this:

import java.io.*;
public class GameHelper {
    public String getUserInput(String promt){
        String inputLine = null;
        System.out.print(promt + " ");
        try {
            BufferedReader is = new BufferedReader(
                    new InputStreamReader(System.in));
            if (inputLine.length() == 0) return null;
        } catch (IOException e){
            System.out.println("IOException: " + e);
        }
        return inputLine;
    }
}

But eclipse tells me the exception is never thrown from the try statement, what does that mean?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Your problem is simple. When the function reaches the end (getUserInput) and returns a value. That value is null because inputLine is set to null and never set to anything else. Do what the other guy said (inputLine = is.readLine();) which reads the next line in the input (in this case the next input line). I am guessing that you are trying to get the next input and display it. This is what you want:

public String getUserInput(String promt){
String inputLine = null;
System.out.print(promt + " ");
try {
        BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
        inputLine = is.readLine();
        if (inputLine.length() == 0) return null;
    } catch (IOException e){
        System.out.println("IOException: " + e);
    }
    return inputLine;
}

The error probably was with the inputLine.length() because inputline is null and inputLine.length() would be an error.

\$\endgroup\$
6
  • \$\begingroup\$ Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unreachable catch block for IOException. This exception is never thrown from the try statement body at GameHelper.getUserInput(GameHelper.java:10) at SimpleDotComTestDrive.main(SimpleDotComTestDrive.java:14) \$\endgroup\$ Commented Aug 5, 2012 at 6:03
  • \$\begingroup\$ I am guessing SimpleDotComTestDrive is the class you put in this question. Which is line 14? \$\endgroup\$
    – rdelfin
    Commented Aug 5, 2012 at 6:10
  • \$\begingroup\$ String guess = helper.getUserInput("enter a number"); \$\endgroup\$ Commented Aug 5, 2012 at 6:32
  • \$\begingroup\$ its strange because I have copied everything exactly from the book, just like it told me to do in this instance. \$\endgroup\$ Commented Aug 5, 2012 at 6:46
  • \$\begingroup\$ Commenting is allowed at about a reputation of 50 ;) \$\endgroup\$
    – Aron_dc
    Commented Aug 5, 2012 at 11:00
2
\$\begingroup\$

The telling part of your code is when you create the new BufferedReader and check to see if the newly-read line is of length zero:

BufferedReader is = new BufferedReader(
                new InputStreamReader(System.in));
        if (inputLine.length() == 0) return null;

Note that you haven't actually read in a line yet. Just add this line between the above two:

inputLine = is.readLine();

This will fix Eclipse's warning and ensure that you're actually reading a line from the input device.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .