Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:24:23+00:00 2026-06-16T04:24:23+00:00

So this is an odd one, I know the code itself is fairly useless,

  • 0

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';    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:24:24+00:00Added an answer on June 16, 2026 at 4:24 am

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might sound like an odd scenario, but I've got two forms on one
I know this questions might seem a little odd, but I want to make
This question may seem a little odd/weird, but I have two UIWebViews, one of
this is a magic square generator, but do not know C++, I have some
Ok, I know this is going to seem odd. but, I am trying to
This is an odd one. I added a file to a project, thought I
This is a very odd one and I'm not sure how to proceed. Basically,
I was wondering if I can make my code clearer by indicating one variable
This is an odd question because I don't know what to call it! I
I have very odd problem and I don't know how to get rid of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.