• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

all confused about scope....

 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just when i start to think that i am beginning to understand this Scope issue with functions, it bites me in the derriere. i have my usual static main() function, which calls a public function declared within the main() function, to print some characters. it is getting more and more frustrating, i have tried moving the starOut() function -- which prints a number of characters per row to complete an outside triangular image. here is the code:
---------------------------------



i would very much appreciate an explanation of what i am doing wrong, and would love an example of how it should be done right...NOT the actual answer for this one. i know this has to be something really simple that i am missing. earlier today i fretted for hours about why a function could not be found, only to realize the spelling of the function was wrong. you know what? i love programming even more because of it
thanks,
thomas
[This message has been edited by Thomas Whalen (edited November 11, 2001).]
[This message has been edited by Cindy Glass (edited November 12, 2001).]
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about the intrusion here, but it looks like my code indentation and line spacing is not being preserved. any pointers on how to better do this?
 
frank davis
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was your error message?
My guess is it something to do with defining a method inside a method. Try moving the Starout method to outside of main and make it static.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Thomas
I guess for starters I would suggest not putting some much code inside of "main". I know that there isn't much code in the first place but putting ever piece of code into the "main" is not doing anything for you.
One real problem is that your starOut method is inside of your for loop.
And you don't need to say "System.exit" right before the code is about to exit normally.
Maybe something like this:

Than have another "driver" class which has a main and just calls PrintTriangle().
[This message has been edited by christopher foran (edited November 12, 2001).]
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To perserve code indentation see "http://www.javaranch.com/ubb/ubbcode.html".
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how much commenting is necessary? i always thought that you want to comment to help yourself out and make things easier on the next programmer that comes along?
the reason i called the starOut method IN the loop is because each method prints part of a triangle. there is more than one method printing an inverted triangle in this program. i guess the only other way to do this is stick each triangle into a multi-array like this?
tri[0][0]...
tri[1][0]...
tri[2][0]...
*xxxxx
**xxxx
***xxx
****xx
*****x
i know this isn't a function program, but i wanted to see if i could do this.
 
Thomas Whalen
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i forgot to mention that i did use the UBB tags , but brackets were still being stuck in the wrong places and statements didn't have the same number of line spaces as they did in my IDE.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Whalen:
how much commenting is necessary? i always thought that you want to comment to help yourself out and make things easier on the next programmer that comes along?

By choosing good identifier names, you can let the code describe what is going on. A good time to comment is if you're doing something unusual. Always ask yourself, "Does the comment add to the readability?"
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Whalen:
i did use the UBB tags , but brackets were still being stuck in the wrong places and statements didn't have the same number of line spaces as they did in my IDE.

I don't know what it looked like in your ide. I don't see a problem with the brackets or the spaces. To preserve blank lines in these forums, put a space at the beginning of the line. Otherwise the UBB software takes the blank line out.


[This message has been edited by Marilyn deQueiroz (edited November 12, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Whalen:
the reason i called the starOut method IN the loop is because each method prints part of a triangle. there is more than one method printing an inverted triangle in this program. i guess the only other way to do this is stick each triangle into a multi-array like this?

Calling the method within the for loop is not a problem. Defining the method within the for loop will not compile.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your comments are fine.
Your code has both the 1] method call and the 2] method inside of the loop. The call to the method can be inside of the loop, that makes sense. Having the method inside the loop is confusing because the code inside of the method will not be executed each time the loop iterates. The code inside the method will only be executed when the method is invoked. In your case the method is invoked each time the loop iterates. (Clear as mud?)
Another common solution to the 'print the triangle' problem is to use nested loops.

If you don't like how your post looks you can also edit it by clicking on the icon that has a little pen/pencil.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Other suggestions:
1) Once you get into the habit that character case is significant in names, it is an easy step to use character case as a "word separator" in multi-word names. The old C habit of using underscores can lead to long names if you try to make them expressive, and just ignoring spaces can lead to words that read very strangely. i.e. number_of_rows becomes numberOfRows.

2) Try to start your for loops at zero whenever possible.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic