First Java Challenge and Getting to Know Variable Scope

Tiffany White,

[caption id="" align="alignnone" width="795"] Image credit: Atypically Relevant http://www.atypicallyrelevant.com/return-to-school-post-haste/headdesk-2[/caption (opens in a new tab)]

So I have stumbled a bit in the intro class, but not too badly. When working with opening text files, writing text to those text files, and then reading the text back into the program, I got stuck with scope. And I knew it. It wasn’t like I was flying blind without knowing my problem which, after a year of doing this, I am pretty proud of. I still couldn’t figure out where my issue was. I went to Stack Overflow, I asked around. For one, this is what my code looked like prior to getting it corrected:

As you can see I was trying to access a variable outside it’s scope

. I declared it twice as well. I knew I needed to change the variable to be outside of all the loops, global (but not quite, as I was told about the resulting code, not sure why) sort of. I would declare it right after the method but the compiler complained that I did not initialize

. And I didn’t. I didn’t know how.

So frustrated, I jumped on the Code Newbie Slack general channel and just threw it out there. Most are web developers but someone helped. Cory. He told me to initialize it with empty single quotes. The compiler didn’t understand the empty single quotes so I changed them to an empty string with double quotes. Here is the final program, which isn’t elegant, but I still got a 100 anyway, as it was just for participation:

Fin. Whew.

© tiff.RSS