• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

If else statements

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Just playing around with if statements and was wondering what's going wrong with the following:


Current output is:

cannot find symbol

and highlights the linkSent variable.
 
James Ritchie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just tried to edit the code so it's all in line and just made it worse, so please don't roast me for the indenting
 
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

cannot find symbol


The linkSent variable is out of scope where the error happens.
Declaring a variable inside of {}s limits its scope to be within those enclosing {}.
The posted code has several local declarations of linkSent (each within a {} )
Add a single declaration for linkSent outside of the {}s in the if statements so it is in scope where it is being referenced.
 
Saloon Keeper
Posts: 1652
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James:

There are at least two serious problems, not related to formatting with lines such as:


1. You should never be comparing String types with ==, instead you should (at very least) be using .equals() so that the test would look more like:
"Y".equals(checkForYN)

2. Your boolean for linkSent is being declared inside the { } block, and goes out of scope immediately upon hitting the closing }

So a working version of what you were trying to do would look more like this:


This might be a good time to ask where you have been learning Java from?
There are countless paid and free sources of information, we can suggest some if you are unsure of what to be consulting.

People here can and probably will answer any questions you come up with while learning, but it would likely be valuable at this point to be following along with a tutorial on Java Basics.

One such free one that millions have used can be found here:
https://docs.oracle.com/javase/tutorial/

 
Saloon Keeper
Posts: 8760
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Ritchie wrote:

Also... The 'else' in line 4 ONLY applies to the 'if' on line '3' so 'linkSent' would end up being false in any case. You'd need to change line '3' to an 'else if' to get the 'else' on line 4 to work the way you want.
 
James Ritchie
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Thanks for your replies! That helped a lot. With regards to where I'm learning, I'm using codecademy.com - which I've heard is quite good.

One of the issues I'm finding is that I'm bringing bad habits into my Java/other languages which I picked up from learning Python. It wasn't until I started looking at other languages that I realised how many bad habits you pick up from it....

 
Jesse Silverman
Saloon Keeper
Posts: 1652
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent.

One good habit you can bring from Python is to indent as if it actually mattered to the compiler, because the reader is just as likely to mis-interpret you java code as the Python interpreter is in Python.

I was rushing because I was working on something else, so I preserved the un-cool formatting in terms of indentation when I corrected your code.  I usually would fix stuff like that.

Try not to mis-lead readers by careless indenting styles, javac and java won't care one bit, but Java Programmers will be confused and grumpy when they (mis-)read it.
 
James Ritchie
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand, the reason for the wacky indentation is because I copied and pasted it from my IDE, and then created the post, then tried to edit it, but made it worse :')

Not sure what happened but here we are - I promise indentation is the least of my worries, I hate reading code without indents.
 
Marshal
Posts: 3720
533
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will probably have better success retaining your formatting when you post here if you change your IDE to indent using spaces rather than tabs.
 
Saloon Keeper
Posts: 24568
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I just had to re-render this for better readability:

I did this manually, but most IDEs have a reformat source code option that can optimize that including options that allow you to pick which way to place the brackets.

I started by addressing the throwaway definitions of linkSent, but actually, this is how it should really look:

We can crunch it down even further, but this is good enough.
 
Marshal
Posts: 74381
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is a simpler way ot do it, without needing an if‑else:-Note the syntax with the () and the not operator ! and the or operator ||. Any changes inconsistent with de Morgan's Ruless will make your loop work incorrectly.
 
Jesse Silverman
Saloon Keeper
Posts: 1652
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way we could phrase how you want to get lexical scoping for things:

You want the smallest scope (the variable is "in scope for the smallest time") that is needed, but not less.

People used to write huge functions with all the variables declared up top in the bad-old-days, that works but is bad for several readability and maintenance reasons.

On the Java Certification exams, they absolutely love having a piece of code trying to reference a variable that is no longer in scope, you can do all sorts of complex tracing to figure out what it will print -- which will be wrong because a reference to an out-of-scope variable will make it not compile, as you saw.

I don't think you are taking one of those exams anytime soon, but they put those questions on there because it is an easy, forehead-slapping mistake to make.
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . all the variables declared up top . . . that works but is bad for several readability and maintenance reasons. . . .

It is also easier to write a compiler to deal with such variables. It is less of a problem if all functions/procedures are kept small.
 
Tim Holloway
Saloon Keeper
Posts: 24568
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It is also easier to write a compiler to deal with such variables.



Not unless you're writing a COBOL compiler. Back in ancient times with limited memory and processing power, you might want to digest symbol definitions in a early phase to make it easier for later processing phases because the symbol table was monolithic and fixed-size. These days you can save resources by limiting the scope of definitions to the blocks that they're needed in.

More practically, you can't trash variables that aren't in scope and for that matter, in some cases you can copy/paste similar blocks of code and keep the same symbol name for each block without running into conflicts. But I get yelled at when I do that!
 
Jesse Silverman
Saloon Keeper
Posts: 1652
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim H. wrote:you can copy/paste similar blocks of code and keep the same symbol name for each block without running into conflicts. But I get yelled at when I do that!



You had me right up to THERE, but I would freak at that too, it is too easy for the reader to get confused.

{ int mountain = 5; }

// more code
{ int mountain = -999; }

"First there is a mountain, then there is no mountain then there is." -- Donovan

Yeah, please don't do dat, dude.
 
Tim Holloway
Saloon Keeper
Posts: 24568
168
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, it's more like this:
 
Jesse Silverman
Saloon Keeper
Posts: 1652
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I worked with code like that for decades and we didn't die or go out of business.

However, there are multiple drawbacks to it, especially when working with reports from static analyzers and various other tools.

There may be an analogy to where you remind people that you can lower-case every class name and upper-case every method and variable name and java will still compile and run it, but many useful tools may trip over that.

I would say that re-using the same variable name for different meanings, whether one declares it only once and keeps re-assigning it over a method-body that is already probably problematically long, or, in some ways worse, has the same name coming in and going out of scope in different parts of the method, is high on the list of "Yes, it is legal, but please don't do that -- you will note the bug I am currently fixing is precisely because another developer misinterpreted the code as it was."

I was more polite about it than some others who probably felt the same way about it, but grew up in environments where being forthright, direct and unambiguous was valued more highly than politeness and delicacy.  Their comments when fixing bugs caused by developers getting confused by this practice can not be reproduced on a Family-Appropriate website such as CodeRanch.  Note that the usages were normally spread far enough out vertically that the opportunities for reading the code wrongly were significantly larger.

Reading the outputs of some very solid static analysis tools was complicated even more.  Normally the reports would be self-sufficient and self-evident, but in the face of this one would have to be paging thru code trying to make sense of it...

The bigger message is "Don't write overly wrong methods and functions!" because in very short ones it is unlikely that this will ever happen.  But in an environment where many people routinely ignored that first rule, also re-using variable names for different purposes mixed like alcohol and sleeping pills.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic