Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Error in block Scope ecample

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error in block Scope ecample

    Hi all,
    I am a beginner in programming and learning java using texteditor. I am getting an error in a program. Please tell me what's wrong in the below code.


    // Demonstrate block scope.
    class Scope {
    public static void main(String args[]) {
    int x; // known to all code within main
    x = 10;
    if(x == 10) { // start new scope
    int y = 20; // known only to this block
    // x and y both known here.
    - 46 -
    System.out.println("x and y: " + x + " " + y);
    x = y * 2;
    }
    // y = 100; // Error! y not known here
    // x is still known here.
    System.out.println("x is " + x);
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Error in block Scope ecample

    When posting code, please use the highlight tags to preserve formatting.

    What exact error are you getting? You have it commented with exactly why you'd get an error, so what are you confused about?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in block Scope ecample

    Quote Originally Posted by KevinWorkman View Post
    When posting code, please use the highlight tags to preserve formatting.

    What exact error are you getting? You have it commented with exactly why you'd get an error, so what are you confused about?
    Hello, sorry for my posting mistakes..!
    // Demonstrate block scope.
    class Scope {
    public static void main(String args[]) {
    int x; // known to all code within main
    x = 10;
    if(x == 10) { // start new scope
    int y = 20; // known only to this block
    // x and y both known here.
    - 46 -
    System.out.println("x and y: " + x + " " + y);
    x = y * 2;
    }
    // y = 100; // Error! y not known here
    // x is still known here.
    System.out.println("x is " + x);
    }
    }

    In above example it is showing me the error in the line 5 and the error is : Scope.java:5: ';' expected
    If(x==10){
    ^
    1 error

    Tool completed with exit code 1

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Error in block Scope ecample

    You still haven't used the highlight tags. But your error might have something to do with the random "- 46 -" you have in the middle of your code?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in block Scope ecample

    I removed it but still it is showing me that error.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Error in block Scope ecample

    Please post the updated code, inside highlight tags, so we're all looking at the same thing. Are you sure you're recompiling the correct file?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Error in block Scope ecample

    You have declared the y variable inside the if statement therefore you cannot access outside the if statement.
    Improving the world one idiot at a time!

Similar Threads

  1. [SOLVED] Out of scope problem
    By lf2killer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2012, 04:28 AM
  2. Scope problem
    By TP-Oreilly in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 9th, 2011, 08:41 AM
  3. try-catch block reports to have an Error that I don't know how to fix
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 24th, 2011, 06:27 AM
  4. Scope of Variables
    By PineAppleKing in forum Java Theory & Questions
    Replies: 5
    Last Post: June 11th, 2011, 10:22 AM
  5. The simpler the program - seemingly the more scope for error :p
    By Bacon n' Logic in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 14th, 2010, 06:10 PM