15

So this is an odd one, I know the code itself is fairly useless, but what I'm wondering why I get the error:

I was writing some code, I had written this:

if(scan.hasNextInt())
    int row = scan.nextInt();

Wasn't thinking about variable scope at the time, obviously this is useless because I can't use row past the if anyway. What I don't get is why I got the error I did:

> javac hw.java
hw.java:25: '.class' expected
    int row = scan.nextInt();
        ^
hw.java:25: not a statement
    int row = scan.nextInt();
    ^    
hw.java:25: illegal start of expression
    int row = scan.nextInt();
            ^
hw.java:25: ';' expected
    int row = scan.nextInt();
                  ^

Now if I just modify that if check to:

if(scan.hasNextInt()) {
    int row = scan.nextInt();
}

It will compile fine. I was under the impression that if there was 1 line under the if the curly brackets were optional... clearly there are other considerations as well or both would either compile or fail.

Could someone explain to me, or point me to a document that explains why I can't declare a local variable under the if conditional without the curly brackets?


EDIT: Here's the full function:

public static char getinput() {
    System.out.println("Where do you want to go? (row column)");
    Scanner scan = new Scanner(System.in);
    if(scan.hasNextInt())
        int row = scan.nextInt();
    String input = scan.next();
    System.out.println(input);
    return 'a';    
}
2
  • 7
    what comes before and after that if? Commented Dec 19, 2012 at 15:58
  • 1
    Please write your complete code here Commented Dec 19, 2012 at 16:00

2 Answers 2

26

If you have a if, for, while, do/while you must follow it with a statement. A declaration is not a statement.

From JLS 14.9 - The if Statement

IfThenStatement:
    if ( Expression ) Statement

IfThenElseStatement:
    if ( Expression ) StatementNoShortIf else Statement

IfThenElseStatementNoShortIf:
    if ( Expression ) StatementNoShortIf else StatementNoShortIf

I assume they do this because any variable you declare couldn't be used as it would be out of scope immediately (except the same declaration)

Sign up to request clarification or add additional context in comments.

3 Comments

There it is. Thank you very much!
@PeterLawrey I'm not 100% sure, but this sentence from JLS 14.4 might be more relevant - "Every local variable declaration statement is immediately contained by a block." (emphasis added)
Changing the if to a while results in the same error, so actually I'm pretty sure that's the rule in effect
1

My guess is that a declaration isn't an executable statement and a declaration with an assignment is really broken into two different statements by the compiler, with the first statement (the declare) not being executable. An if requires an executable construct, either a block or an executable statement.

I know that I've not been able to put a breakpoint on a non-sassignment declaration statement in the Eclipse debugger. Probably the same underlying reason.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.