#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Small code for Volume issue.


    I am very green to java so please be patient

    I am not yet looking for the code. I would rather have some directional pointers on how to fix what my issue is.

    What I need to do is:
    (hopefully I get the format right to use code)

    PHP Code:
    import javax.swing.JOptionPane;
     public class 
    ObjectVolume 
     
    {

        public static 
    void main(String[] args)
        
        
    //Write a program that accepts as input the mass (in grams) and density
        //(in grams per cubic centimeters), and outputs the volume of the object using
        //the formula: density=mass/volume. Format your output to two decimal
        //places
        

            
    String massInput;
            
    String densityInput;
            
    String inputStr;
            
    String outputStr;
            
            
            
    int mass;
            
    int density;
            
    int volume;
            
            
    massInput JOptionPane.showInputDialog("Enter the Mass in Grams");
            
    densityInput JOptionPane.showInputDialog(" Enter the Denisty in Grams");
            
            
    volume mass*density;
             
            
    outputStr " Volume in Grams: "  String.format("%.2f"volume );
            
        }


    Feel free to tell me I am way off base on this.

    This is the exception thrown:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The local variable mass may not have been initialized
    The local variable density may not have been initialized

    at ObjectVolume.main(ObjectVolume.java:26)
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,534
    Rep Power
    1906
    What is the value of mass and density?

    You have to convert the user input (massinput/densityInput) to integer.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    crud , i will go check that now thank you.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0
    this atleast got me running :

    [php]
    inputStr = JOptionPane.showInputDialog("Enter the Mass in Grams");
    mass = Integer.parseInt(inputStr);
    inputStr = JOptionPane.showInputDialog(" Enter the Denisty in Grams");
    density = Integer.parseInt(inputStr);

    volume = mass*density;

    outputStr = " Volume in Grams: " + volume ;
    JOptionPane.showMessageDialog(null, outputStr,
    "Volume is :",
    JOptionPane.INFORMATION_MESSAGE);
    }

    [php]

    I believe this is correct.
  8. #5
  9. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,534
    Rep Power
    1906
    That is correct.
    Now try to enter some text in the input and see what happens.

    You have to look up try-catch as parseInt throws an exception if input is not a correct number.

IMN logo majestic logo threadwatch logo seochat tools logo