• Post Reply Bookmark Topic Watch Topic
  • New Topic

while loops & variables  RSS feed

 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help!

I'm using two WHILE loops, & the program refuses to recognize one of my variables when I want to print it:


The program says that it doesn't recognize numOfNegs in the system.out statement. I've tried moving those daggone brackets around, & if I move all the closed brackets to the end, and then the program recognizes my variable, but it won't do the system.out.print. I'm doing it in NetBeans, which tells me that I can declare it locally, & when I choose to let NetBeans do that, it declares numOfNegs as a string.

Can anyone help???
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Karen!

If you are posting a program, please UseCodeTags (← that's a link). It makes the code a lot easier to read.

Your problem is one of scope and logic. numOfNegs is declared inside a while loop, so it will only have scope in the loop, not outside of it. But also, logically, you want the initialization to be outside the loop anyway, or you will just be setting numOfNegs to zero over and over again.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help & the protocol tip.

I'd already tried initializing numOfNegs just before the second WHILE loop, & then the recognition isn't a problem, but then it won't count my < 0 (negative) integers. At one point, it was counting them, but something else went wrong at that point. (During my game with those daggone brackets.) What I think I'm telling it to do is to increase the value of numOfNegs, which is initialized at 0, by 1 every time a negative integer is entered. Either that's not what I'm doing, or something else is overriding that.

MORE help?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I added the code tags, and doesn't it look better Always use the tags. But it does show that you have inconsistent indentation, which will make your code hard to read, and also makes it harder to find errors. It is usually harder to find errors than to correct them. I think you ought to go and correct the indentation and post the code again. It is usually a bad idea to use an IDE at your stage of learning, but if you are, you can highlight the entire class with ctrl‑A and then use ctrl‑F or ctrl‑I to improve formatting. You will then see what the relationship between the two loops is.
If you are not using an IDE, get a decent text editor, e.g. Notepad++ (not the Notepad which comes bundled with Windows) and set up the following options:-
  • Syntax highlighting (if you are not dyslexic and have normal colour vision).
  • Bracket matching
  • Automatic indentation
  • Automatic conversion of tabs: 1 tab→4 spaces
  • Now go through the code and remove all the leading whitespace from each line. Then startinng from the beginning, repeatedly push the end→del[ete]→enter keys until you reach the last line. If you have passed an open brace { push the tab key once; if you have just passed a close brace } push backspace ×4. Yes, I know that is tedious but you need to beat your code into submission so it is serving you not you struggling to understand it.

    As I said, once you get the indentation right you will be able to see where the two loops are.
     
    Knute Snortum
    Sheriff
    Posts: 4288
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    After you have done what Campbell suggested, please post your code, thanks!
     
    Karen Guffey
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks, guys. I'll be back when I've found & completed a lesson on indentation. I use Netbeans, but when I used Campbell's instructions, it showed the same formatting as is on my posted program after Campbell put the code tags in.

    In my current life, I'm a linguist, & I have to tell you that natural languages are a lot more forgiving than programming languages!
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Maybe the instructions I gave were wrong; that is what you would do on Eclipse. That is why we discourage beginners from using IDEs: there are so many instructions and options that you can spend as long working out the options for the IDE as you do thinking about the programs.
    Natural languages are not forgiving. You are writing them for something different, the brain. It has much greater powers of interpretation than a computer. In a written piece of English you can cause confusion by putting stops and commas in the wrong place. You are here trying to find where the commas are. The difference between the brain confronted with misplaced commas and a computer confronted with misplaced commas is that the computer will believe you!
     
    Karen Guffey
    Ranch Hand
    Posts: 87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I did it, I did it, I DID IT!!! I changed the second "while" loop into an "if"statement & included it in the first "while" statement--& it works!!!

    And you're right, Campbell--it's the human brain that's more forgiving than a computer, not the language!

    I wish I had more time to work on Java, because it's fun, but I have to keep my day job for 10 more years. So I'm limited to about 12 hours a week. Rats!

    Thanks, guys!
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!