[Logo]
Forums Register Login
Having trouble getting calculations to return output
I Have no errors in my codes but I do have warnings. In my output everything shows up fine but the area and cost will not calculate appropriately. The calculation and output starts from line 7 to line 23. Can someone please inform me on what I am doing wrong, thanks.

 

but the area and cost will not calculate appropriately

Area calculation tested, worked fine
When I enter the area and cost, the dialogue box from the output comes back and says 0 for both. I am using NetBeans, would that make a difference?
Where you  enter the area and cost ? Not enter length and width ?
I apologize it's been a long day. Yes, when I enter the length and width I want it to calculate and show up on the output. The area is supposed to show the length * width calculated, and the cost is supposed to output area * 20 id wood is selected and area * 10 if carpet is selected.
But no matter what length and width I enter, the cost and area both come back equaling 0 on the output instead of the correct calculations. 
In line 89 you calculate an area. You've got some debugging code there so I can assume that line 89 is being executed, no?

But your complaint is that you don't see that area appearing in the GUI anywhere? That's because you don't have any code which writes it to the GUI. I'd expect to see you calling the setText() method of some component to put the computed area into that component.
(1 like)
Oh, you're popping up a dialog box to display the relevant info. And yes, the area and cost values are zero because you set them to zero in lines 82 and 83. If you want them to be preserved from one call of actionPerformed() to the next call (and you do) then they should be instance variables of the class.
(1 like)
  • As Paul pointed, you need to declare those area and cost as instance variable in the class rather than in that method.
  • Look at the String literal you're assigning to an instance variable flrType of type String in method itemStateChanged() and the String literal you're checking for equals in case label "Calculate the Cost" of switch block.
  • It is better to use braces although If statement has only one line of code.
  • Like
  • Write validations for user input values, what If user won't enter value Or enter invalid information and press Calculate the Area button.
  • Did you notice you can choose both radio button simultaneously? You need to create a ButtonGroup instance and add each radio button to it. Please go through How to Use Radio Buttons
  • (1 like)
     

    Snipe Anthony wrote:But no matter what length and width I enter, the cost and area both come back equaling 0 on the output instead of the correct calculations. 

    Use final with global variables
     

    John Joe wrote:Use final with global variables

    area as final, are you sure? then will compiler allow to reassign new value to area on line number 89?
    Thank you all so much!
    I declared area and cost as instance variables in the class and area is being calculated but cost is still not shoeing up in the output. Also, Ganesh how would I go about splitting the radio buttons so they cannot be chosen at the same time?
     

    Snipe Anthony wrote:area is being calculated but cost is still not shoeing up in the output. Also, Ganesh how would I go about splitting the radio buttons so they cannot be chosen at the same time?

    Please read my previous comment.
     

    Ganesh Patekar wrote: area as final, are you sure? then will compiler allow to reassign new value to area on line number 89?

    Remove the final keyword
    (1 like)
    Line no 70-73, you're assigning either flrType="Wood was Selected" Or flrType="Carpet was Selected" according to radio button chosen and on line no 93, 95 checking flrType.equals("Wood") same goes for Carpet so both condition fails and cost remains 0.
    For grouping radio buttons please go through the link I provided in previous comment, read the code under comment //Group the radio buttons.
    (1 like)
    Welcome to the Ranch

    Snipe Anthony wrote:I Have no errors in my codes  . . .

    You mean the compiler doesn't show any errors. I am afraid I can see a lot wrong with your code.
  • 1: Your indentation is incorrect. Most of your code should be one level of indentation to the right. Also a few lines (99 and 102 and less so line 89) are much much too long and ought to be divided into separate lines. Otherwise it isn't possible to read the whole of them on this web page.
  • 2: Don't declare multiple variables on the same line. I also think most of your variables can be converted from fields to local variables.
  • 3: Your variable names are not helpful. Who knows what lab1 and lab2 mean? In fact it is possible to read those names and not even know they are labels.
  • 4: I think it is a bad idea to subclass most display components, including JFrame. You can find an example of how I think it should be done here. Note that I have written that little example with every component as a local variable.
  • 5: I think you should have a separate Order class with sizes and materials as fields. Make that class do all the calculations for area and price. Get that class to work before you put a GUI atop it.
  • (1 like)
    I appear to have pushed the submit button rather than the preview button by mistake, and unfortunately I am in Magnus Magnusson mode:-

    I've started, so I'll finish.

    I think another error is making a frame implement listener interfaces. I think that is incorrect object orientation, and also you end up with error‑prone if‑elses or switches.

    What I would do is write an enumerated type/enum with members like CORK and LINOLEUM. I usually don't like action commands, but I can see a way to make the object oriented in this instance. There are other ways to link buttons to types of flooring; this is one way to do it. Maybe somebody else will have a better suggestion.
    Have radio buttons called corkButton and linoleumButton or similar.
    Set FlooringType.CORK.toString() as the action command.
    Use a separate class as the FloorTypeListener (implements ActionListener). Get the action command from the event and use that to call FlooringType.valueOf(). That way you can get away without writing any String literals. Trying to write the same text several times is error‑prone.

    Have separate listeners for the buttons which calculate the price. If you go through the Java™ Tutorials link I quoted above, you can find out how to add fields to the enum elements, e.g. price per unit area.
    (1 like)
    This is more like what your code should have looked like:-I have added indentation to the main method, some empty space to move line 99 to space 99, and some line ends and more spaces in the appropriate locations. I only corrected two places where you should have had more space: before the * in line 89 and after the comma in line 115. All your commas should be followed by a single space (or new line) and similar spacing should apply to operators like + or *. The lack of spaces makes it difficult to see that "Information" is an argument in its own right, for example. You would have written the line breaks a lot better if you had done it yourself. I shall give you the opportunity to try line breaks by permitting editing of your first post.
    Only use null as the first argument for a dialogue method if you don't have a GUI. Then the dialogue will appear in the centre of the screen. If you have a GUI, use the name of one of the components, and the dialogue will appear in front of that component.

    You should be able to work out there is something wrong with the actionPerformed method because apart from its being too long, it doesn't do one thing. Sometimes it displays something on a dialogue and sometimes it prints something to System.out. That shows it ought to be divided into several methods. Another thing about System.out. If you have a GUI, it is possible to run the app with the javaws tool rather than java. If you do that, there will be no visible console and nothing printed to System.out will be visible.
    (1 like)
    When copying your code, I also removed the empty lines on each side of the { and } surrounding the main method. You don't need empty lines there, and they don't enhance legibility.
    Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


    This thread has been viewed 253 times.

    All times above are in ranch (not your local) time.
    The current ranch time is
    Jan 19, 2018 10:25:27.