File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local variable

 
Ashish Paliwal
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class A {
public static void main(String args[])
{
String s = "ashish";
t = s.length();
while(t.length > 3)
int i;
}
}

I know this code is not doing anything useful. But my doubt is if I declare any variable inside while loop like this one, then it gives me 4 errors in that line. Doesnt matter which refrence variables you are using int or List.
More strangely, if I put { } in this one statement while loop then it works.
I a absolutely puzzled ??

I am sorry if this sounds too simple for people but I am newbie.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what are the errors you are getting? Have you declared 't' somewhere?
 
Ashish Paliwal
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the only code. Only variable declared in whole program is t.

Errors are :

1) '.class" expected
2) not a statement
3) illegal start of expression
4) ; expected

all the errors are in that line only "int t;"

Can someone please try this small program and let me know if this is working. As I told, strangely enough if I put brackets in this one line while loop, it works.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags and post real code. With the code you gave us (which you probably retyped) I see so many more problems:
1) Class starting with a capital C
2) t is never declared
3) t is obviously meant to be an int yet you try to call its length field like it is an array

So please copy/paste the real code and put them in code tags to preserve the formatting.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh I see. length is not a method in String class. please read more about String's length and Array's length()
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of things wrong with the code.

First, let's put it between code tags, so that it's easier to read and line numbers are added automatically (note, please use code tags yourself next time when you post code):

Problems:

  • Line 1: It should be "class" with a lower-case "c", not "Class" with an upper-case "C".
  • Line 5: You didn't declare the variable t. You probably meant: int t = s.length();
  • Line 6: If t is an int, then it doesn't have a property called "length".
  • Line 7: Why are you declaring an unused variable i here? Did you mean to put more code in the while loop?


  • Rahul.p Kumar wrote:ohh I see. length is not a method in String class. please read more about String's length and Array's length()

    There is a method called length() in class String. Look it up in the API documentation.
     
    Ashish Paliwal
    Greenhorn
    Posts: 9
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Doesn't matter. Try with length() or with while(true) or with any condition....
    it is not working
     
    Ashish Paliwal
    Greenhorn
    Posts: 9
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am sorry for undeclared t and capital C. I just wrote rather than copy pasting from IDE.

    My point is :

    even this

    while(true)
    int i;

    is showing errors. And if I put braces around while loop, everything is working. Say above 2 lines are only thing inside main method. Then also it is not working.
     
    Ashish Paliwal
    Greenhorn
    Posts: 9
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is the actual code

     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47229
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are not allowed plain declarations like int i; inside a loop. At least I think so; that is one of your compiling problems.
     
    Ashish Paliwal
    Greenhorn
    Posts: 9
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, then why does it work after putting braces around while loop.
    Also, its not about int i, declare any variable, List l or Object o;

    It will give errors.

    I am not able to figure put what's the problem
     
    Ashish Paliwal
    Greenhorn
    Posts: 9
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, probably I figured out the problem.
    There is no block scope for that variable. Variables are always declared in some blocks. So that is why it was working with braces.
    It happens same way in C also.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47229
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't think you have figured it out. No, it doesn't work the same way in C at all.
    If you have previously written C you need to forget it; the rules about scope are completely different.
    No, it is not a scoping problem; it is that loops have to contain statements (at least I think they do; you would have to check the Java™ Language Specification for the full details of the grammar). But int i; is not a statement, but a declaration. So it is not permitted inside a loop.

    At least I think that's the problem; when I tried your code I got a "not a statement" error for that line.
     
    Ashish Paliwal
    Greenhorn
    Posts: 9
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hmmm probably right.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20372
    44
    Chrome Eclipse IDE Java Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ashish Paliwal wrote:Ok, then why does it work after putting braces around while loop.
    Also, its not about int i, declare any variable, List l or Object o;

    It will give errors.

    I am not able to figure put what's the problem

    The body of statements like if, while and try must be one single statement. Your "int i;" is not a statement but a declaration. By putting it in braces {}, those braces become a compound statement - the entire block is one statement within which you can basically start over. Both declarations and statements can go inside the block.
     
    Ashish Paliwal
    Greenhorn
    Posts: 9
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So, if I write int i = 10;
    then this is a definition not a declaration. Should this work without braces of while loop ?
     
    Rahul P Kumar
    Ranch Hand
    Posts: 188
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ashish Paliwal wrote:So, if I write int i = 10;
    then this is a definition not a declaration. Should this work without braces of while loop ?


    good question.
    I think the problem is of scope of the variable 'i'. Local variable finds its scope within curly braces. When you declare or define 'i' in one line 'while' statement, 'while' has no problem, but JVM has problem fixing scope of 'i', while it can not attach scope of 'i' to curly braces of main(), it is unable to find braces for 'while' statement as well. This is why as soon as you are putting curly braces, everything works.
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20372
    44
    Chrome Eclipse IDE Java Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, because it's still not a statement. It is a declaration (int i) with immediate definition (= 10).
     
    Rahul P Kumar
    Ranch Hand
    Posts: 188
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob Prime wrote:No, because it's still not a statement. It is a declaration (int i) with immediate definition (= 10).


    Can you explain what consists statement? I really need clarification about this term.
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47229
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Use the same Java Language Specification section I quoted earlier. Scroll up and read it. It is very accurate. It is very easy to read. The last sentence was a lie.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic