Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
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) –  user11177 Aug 5 '12 at 6:03
    
I am guessing SimpleDotComTestDrive is the class you put in this question. Which is line 14? –  rickyman20 Aug 5 '12 at 6:10
    
String guess = helper.getUserInput("enter a number"); –  user11177 Aug 5 '12 at 6:32
    
its strange because I have copied everything exactly from the book, just like it told me to do in this instance. –  user11177 Aug 5 '12 at 6:46
    
Commenting is allowed at about a reputation of 50 ;) –  Aron_dc Aug 5 '12 at 11:00

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.