0
import javax.swing.*;
import java.text.*;

public class UG3 {



    public static void main (String[] arg) {



            String radius = JOptionPane.showInputDialog("Input Radius");
            String height = JOptionPane.showInputDialog("Input Height");// input radius and height.

       try {  // trying to make sure that the user inputs information otherwise it will cancel.

            int rad = Integer.parseInt(radius);
            int hei = Integer.parseInt(height);


            NumberFormat r = NumberFormat.getInstance();

            r.setMaximumFractionDigits(2);
            r.setMinimumFractionDigits(2);

            JOptionPane.showMessageDialog(null, "The volume of the cylinder is approximately "+ r.format(volume) +"CM^3"); // output results. // Here is the problem " volume " is: "cannot be resolved".   



            int yes = JOptionPane.showConfirmDialog(null, "Would you like to find the volume of a cylinder once more?", "", JOptionPane.YES_NO_OPTION);

            if(yes == JOptionPane.YES_OPTION) { 
                main(arg);
            }

            else  {
                JOptionPane.showMessageDialog(null, "You'r welcome! \n \n Click 'OK' to exit", "Cylinder.", JOptionPane.ERROR_MESSAGE);
            }
        }


        catch (NumberFormatException e) { // cancel.

            JOptionPane.showMessageDialog(null, "Sorry but you have to enter radius and height to complete the task."); 
        }




     } //main

     public static double volume(double radius, double height){ 
         return Math.PI * Math.pow(radius,2) * height; 

     }  

}//Cylinder

I wrote "comments" to show you where. Anyway, "volume cannot be resolved" why? Did I miss to delete something from the first method? is something missing? Can't get my head around it. :S

Tom
  • 16,842
  • 17
  • 45
  • 54
Aiham Al-salehy
  • 113
  • 1
  • 1
  • 7
  • 2
    You never parsed/converted radius and height into a number (before the if statement). In fact, you *should* be getting an error `bad operand types for binary operator '>'`... is that what you're getting? *Always* include the error if you have one. – tnw Jul 02 '15 at 17:05
  • thanks! but I managed to solve it with try -catch :) How do I solve the volume calculation in a new method? :D – Aiham Al-salehy Jul 02 '15 at 17:10
  • Did you try just using the method you already have defined?? – tnw Jul 02 '15 at 17:11
  • Yeah but every time I try to move my calculations to a different method I get a lot of errors. saying in line 26 "r.format(VOLUME)" volume is undefined. – Aiham Al-salehy Jul 02 '15 at 17:15
  • I don't see what that code has to do with your `getVolume` method. What are "a lot of errors"? As I said before, if you're getting errors, tell us what they are. – tnw Jul 02 '15 at 17:28
  • volume cannot be resolved to a variable – Aiham Al-salehy Jul 02 '15 at 20:13
  • "JOptionPane.showMessageDialog(null, "The volume of the cylinder is approximately "+ r.format(VOLUME) +"CM^3");" – Aiham Al-salehy Jul 02 '15 at 20:13
  • Show your actual code in your question please. Java is case sensitive, `VOLUME` is not the same as `volume` which IS defined. – tnw Jul 02 '15 at 20:18
  • I have edit my post, sorry about that. – Aiham Al-salehy Jul 02 '15 at 20:27
  • I resolved the issue, Editing my post soon. – Aiham Al-salehy Jul 03 '15 at 11:18
  • I rolled back your question to a version with the "original question" (after some changes though). Please don't edit your question to tell us that you've fixed something. Either mark the answer which helped you to do that as "correct", or post an answer yourself to explain what you've did to solve your problem. Also don't edit your question to ask something new, create a new question instead. – Tom Jul 05 '15 at 21:26
  • I'm sorry. Didn't mean to upset anyone. – Aiham Al-salehy Jul 06 '15 at 21:44

1 Answers1

0

radius and height are String variables, but you're trying to compare them with int values (e.g. radius >= 1).

If all you're trying to do here is to make sure that the user gave you some information, you could do a simple test for null and/or the empty string (e.g. radius != null && radius != "").

* Edit to add do-while *

  1. Put the do-while around the try-catch.

  2. Use yes == JOptionPane.YES_OPTION as the while condition.

  3. Get rid of the

    if (yes == JOptionPane.YES_OPTION)
        //....
    else
        //....
    

    logic.

Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
  • 1
    I solved it with try [{} catch (NumberFormatException e) {} – Aiham Al-salehy Jul 02 '15 at 17:11
  • I tried to do with do-while and tried to the code you gave me but it didn't work. :S any other suggestions how i can make a do- while work? witht he program I have? I have edited my code! =) – Aiham Al-salehy Jul 03 '15 at 12:05
  • 1
    `radius > ""` Checks for a nonempty string? Looks more like a syntax error to me :(. – Tom Jul 05 '15 at 21:29
  • Thanks @Tom, fixed. It's OBE at this point given the OP is using try/catch, but yes, you are correct. – Erick G. Hagstrom Jul 05 '15 at 22:57
  • 1
    I don't think that this `radius != ""` will ever be true, since I don't think that `showInputDialog` returns String literals. – Tom Jul 05 '15 at 23:05
  • 1
    Perhaps, @Tom. Perhaps `!radius.isEmpty()`? Three strikes and I'm out, so fingers crossed. – Erick G. Hagstrom Jul 05 '15 at 23:17
  • 1
    Yes, that would work. Important to know is this: [How do I compare strings in Java?](http://stackoverflow.com/q/513832), but since `!radius.equals("")` looks kind of "silly" it is better/prettier to use `!radius.isEmpty()`. – Tom Jul 05 '15 at 23:22