Forums Register Login
Two codes, one works, one doesn't
Hi new here, new to Java, new to programming in all ways outside of excel calculators and acad macros.
I've been learning for a few days, I wrote a temperature conversion program following a video.  I then wrote a unit converter for millimeters, inches, kilometers, miles just for practice.
The first way I wrote it, well, works.  I went to condense it down and strip away the non essentials to get a better understanding of the individual parts.  I have a better understanding.  But the new code doesn't work correctly and I haven't the slightest idea why.
I've tried several things which I will write below, but first, some copy / paste:

First code: this one works:


Second code:  this one will only output results for the bottom group; doesn't matter how many groups there are, only the bottom works.  And by group I mean the group of like equations;ie //inch input or // kilo input.
Some more history, this is all on a jframeform.  I have 4 text fields.  They all read "0" initially.  When I enter a value into any one field and press calc those if statements and equations run, outputting results into the 4 text fields.
In this second code, any data I enter as millimeters results in all fields become 0.000000.  The inches work fine, all four inch equations.  if I add back in the kilometer group, it will work fine but the milli and inch group will not.
It's like the last group runs with a "0" place holder and outputs an overriding value.  Anyway here it is, the failing code:

So hopefully through the forum I can get a grip on this.  I have lots to learn, but this I don't think is related to core java language.  This is an order of operations or formatting issue I guess and I'm thinking someone can show me the error of my ways on it.

The things I tried you can basically see in the code.  After I removed the try-catch, I declared all my variables in one spot and then ran the program to find it didn't work.  I want to get this to work without the try-catch which I can't imagine is essential to the code running correctly (provided I enter data correctly).
I removed half the code, to make debugging easier.  I changed from "if(!", which should negated the .equal("0") to just a regular if statement and ran a {} else "the equations".  Originally you can see that wasn't the case.
I added an equation to calc the text field being used.  Like I enter data in millimeters and then it equates that entry to the double mm variable and sets it equal to the double rmm result.  On that note , I then busted up the result variables, that was the last thing I did actually.

I don't know if anyone has the time to dissect that, maybe its quick and easy for someone with more experience.
If I'm asking too much just say so, this is NOT important, it is just learning and practice.  I just seems to be less related to language and code and more related to some quirky order of things that I did that doesn't work.

Well here is the answer.  I haven't devoted a lot of time to evaluate why this works exactly.  After reading my post, I saw that I made two changes at once when the program broke.  In the moment I just didn't think to go back and isolate those two things.
So I removed the try-catches here.  Netbeans told me I had too many instances of variable "double r".  I guess the try-catch shielded that in some way.
I removed the second, third, and fourth "double r" declarations and tada!  The program works again, no try-catches.

Here it is in the chance anyone else could learn from it:  I included the clear button and exit button too; just cause

Welcome to the Ranch, Wayne.

Just to improve the chances of more quality answers. Could you please narrow down the problem, however, if you decide not to, please specify exact line numbers you are referring to. Currently isn't clear which code is being discussed and what is the problem you are trying to solve. Usually is best to discuss only one small snippet of code, so people can follow.
Thanks Liutauras Vilda,
I tried to keep it concise.  The problem was that, although the program ran and the dialog box came up for me to enter data, when I would enter the data for millimeters, the results would all be "0.000000".  The last group of equations worked so in the example code I pasted that was Inches.  I could enter data in the txtInch text field and the results populated correctly in the other text fields.  Why one set of equations worked when the other set did not work was the problem.

It seems it was solved by declaring only the variables needed for a set of equations.

But yeah, I know that was a question that could not be focused on one or two lines.

Sorry for the confusion.  But hey, my problem is solved now so maybe it will be useful to someone in the future.

Wayne Barrett wrote:So I removed the try-catches here.  Netbeans told me I had too many instances of variable "double r".  I guess the try-catch shielded that in some way. ... I removed the second, third, and fourth "double r" declarations and tada!  The program works again, no try-catches.

This is a scope thing, not an order or formatting thing. The scope of each of the double r declarations you had was only within the try-catch block that surrounded it. Even though you used the same name, r, each declaration had different and non-overlapping scopes and this enabled you to "reuse" same name over and over again.

When you removed the try-catch blocks, all of double r declarations that were once boxed into their own scope were put into the same scope. You can only declare the same name once in any given scope, that's why you got the "Too many instances of variable r declared" -- the message is telling you that you are trying to re-declare the same variable within the same scope. So, removing the extra ones like you did resolved that problem.
There is a lot still wrong with your code, if you want to investigate further.  I'll start with the most important.

* What happens when you enter "abc" or any non-numeric input into the measurement fields (like txtMilli)?  You will find you still need the try/catches, but only surround the statements with the Double.parseDouble() method.

* When you do this, you will probably run into a scoping problem.  This was the cause of your "too many rs" problem.  Scoping can be a difficult concept to understand, but a good rule of thumb for a beginner is that a variable is in scope (can be accessed) from the time of declaration to the matching closing brace.  To illustrate, consider this code:
x is declared inside the try block, so it's not accessible outside the block (after the matching close brace).  You need to do something like this:
x is declared outside of the try block, but inside the run() method (which is also a block), so the print on line eight will compile.
As for the error of your ways, there are a lot. This method is way too big. It's too complex. There are way too many things happening in this one method. It has way too many independent if-statements. You can't understand what it's doing just by skimming through it; you have to sit there and stare at the code for a while, and pick it apart bit by bit, line by line.  This is what's called opaque code. It's not clear enough to see right through and understand what it's all about.

I have to go out right now and do some things but I'll try to post some cleaner code for you when I get back so you can compare what you did with respect to the laundry list of "code smells" I listed out above.

This thread has been viewed 238 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 18, 2018 12:46:59.