This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Here i am again with if/else statement now Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Here i am again with if/else statement now" Watch "Here i am again with if/else statement now" New topic
Author

Here i am again with if/else statement now

Michael Humphreys
Greenhorn

Joined: Dec 22, 2010
Posts: 23

Well, I am still struggling with the control statements in my program., and I am now getting an error on the "main" method . I am lost with both errors. I just don't understand what I am missing. I do appreciate all of the help i have gotten I hope you don't get tiered of me. (smiles) Here is my code





Thanks again for your help.

Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
You can usually find these types of problems by formatting your code. Here's my attempt. Notice the paucity of braces at the bottom. Also each one of your if statements have the same flaw. Do you recognise it?
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Also, having a member variable yield and a local variable yeild (spelling error) is what they call a "bug waiting to happen" if it isn't already
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

check your semi-colons.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Michael Humphreys
Greenhorn

Joined: Dec 22, 2010
Posts: 23

Hi Tom I have looked and looked at this code till I am blue in my face....I can not for the life of me figure out what it is. and I didn't conceder your other point yet I have to figure out this if/else statement. It is my next step tho. Thank you for your comments Tom.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
One problem is you have an extra brace at line 38.
Michael Humphreys
Greenhorn

Joined: Dec 22, 2010
Posts: 23

Thank you Tom I found thew extra brace. I took a look at the semi-colons and I have one for each statement so am I to assume that I don't need one or more???
Thank you again
Michael
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Here's one of your if-statements:

It says "If yield-cost1 is equal to 0, then don't do anything".

I seem to recall posting this before, though. Was that on one of your threads? Anyway go off and look at some example code written by other people. A tutorial, for example. Does that other example put a semicolon in that position?
Dhruva Mistry
Ranch Hand

Joined: Nov 21, 2008
Posts: 67

Hi Michael,

You needed to remove extra un imp commas after if and else statements & before the if block starts with an open curly brace. and some extra ending curly braces.
apart from that, here is the code you may need :



Dhruva
Michael Humphreys
Greenhorn

Joined: Dec 22, 2010
Posts: 23

Hi all,
I want to ask a question of etiquette on a forum. Should I keep this thread going as I sill have questions in regards to this program although some issues have been solved Others come up as a consequence of those we have talked about already. I guess what I am asking is should i start a new thread for each new issue or continue this thread and add new issues as they come up. Thanks for all the help so far.
Michael
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
I suggest you keep this thread for questions arising from previous questions. It is a bad idea to have two threads about the same thing.

Beware of long lines in code tags; they are difficult to read. Use space not tab for indentation; if you have a decent text editor (eg NotePad2, NotePad++, jEdit) you can set an option to convert a tab to 4 spaces.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Beware: you have a local variable called yield and one called yeild. You are only using one, so should delete the other.
Michael Humphreys
Greenhorn

Joined: Dec 22, 2010
Posts: 23

Hi all,
I would like to thank Dhruva,Paul,and Tom for their help on this issue. I have spent some time and worked out all the issues that we have talked about so far. I sill have one issue remaining. As the program runs it is supposed to evaluate the first "if then else" flow control. If the statement is true then print 0.00. If false move to the next control flow statement and evaluate the "if-then-else" If true print Profit2. If false move to the next statement and print loss.

Now when I run the program all works until i have a "loss" which causes the second control flow statement to print a negative amount for profit2, instead of moving on to the third flow control statement and printing a loss. If you would like to see the code here it is followed by the output.


OUTPUT:

Number Of Chickens =110
Number Of Hens = 100
Number Of Eggs Laid = 3300
CostOf Feed =400.0
Profit = -56.25
You Made A Profit. Way To Go!!!

Once again thanks for all the help.

Michael
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
I have tried to improve your indentation; incorrect indentation makes code hard to read and risks errors with {} not being matched.
Never write == true. Just delete all the == trues in your code.
Never write == false. Change if (b == false) to if (!b) . . . Remember you may bneed () after the ! to maintain precedences
You have one else if followed by {}. Why are you using an empty block?
You are using == 0.00. Remember you may never reach 0 using floating-point numbers because of their inherent imprecision.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
You still need to check the spelling of yield and yeild.
Michael Humphreys
Greenhorn

Joined: Dec 22, 2010
Posts: 23

Hey Hey thanks Campbell I got it you gave some great pointers. One thing I have learned is that I have to READ more closely. I am making stupid mistakes. Any way here is the code:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
You're welcome
You seem to have a != false in there somewhere; you should delete that as it does nothing at all.
Rather than yield - cost == 0, try yield == cost. As I said earlier, you may not get that exactly, because of imprecisions in floating-point arithmetic, so try
if (Math.abs(yield - cost) < 0.005) . . .
Your breakEven in that case ought to be 0; since you only use the breakEven value in that single if block, you may be able to dispense with it and simply write 0. Note you won't get 0.00 printed unless you use a special instruction. There are several possible instructions, of which printf( . . . %.2f . . . ) might be the easiest.

That means you are allowing up to ½ cent as a margin for errors and still regarding the profit as zero.
Michael Humphreys
Greenhorn

Joined: Dec 22, 2010
Posts: 23

Happy New Year Everyone!!

I have read all of your comments, thanks again Campbell, and came up with a working copy of code. (Campbell, I haven't addressed the issue with the Break even point yet but that will be my next refinement.) I have learned a lot from you all. TY Here is my code:

 
GeeCON Prague 2014
 
subject: Here i am again with if/else statement now