Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Errors with beginning jOptionPane code

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Errors with beginning jOptionPane code

    My assignment is to use jOptionPane to rewrite a program that we made earlier, I am successful so far except for the lines 51-53. Those used to be error free but after rewriting main using jOptionPane they are getting errors.
    import java.util.Scanner;
    import javax.swing.JOptionPane;
    public class area {
     
     
        public static void main(String[] args) 
        {
            //Declaring Variables.
     
     
     
            double triOutput;
            int rectOutput;
            double circOutput;
     
     
     
     
     
     
            triOutput = 1;
     
            circOutput = 1;
            rectOutput = 1;
     
            Scanner reader = new Scanner (System.in);
     
            //Receiving Input
            System.out.println ("Which shape do you want area for?");
            System.out.println("1 Triangle 2 Circle 3 Rectangle 0 None");
            String whatShape = JOptionPane.showInputDialog("Shape Number?");
            int shapeNum = Integer.parseInt(whatShape);
            if (shapeNum == 1) {
                String whatBase = JOptionPane.showInputDialog("Triangle Base?");
                double triBase = Double.parseDouble(whatBase);
                String whatHeight = JOptionPane.showInputDialog("Triangle Height?");
                double triHeight = Double.parseDouble(whatHeight);
     
             }
           if (shapeNum == 2) {
     
                String whatRadi = JOptionPane.showInputDialog("What Radius?");
                double circRadi = Double.parseDouble(whatRadi);
            }
           if (shapeNum == 3) {
                System.out.println ("Length of rectangle?");
                String whatLength = JOptionPane.showInputDialog("What Length?");
                int lengthRect = Integer.parseInt(whatLength);
                String whatWidth = JOptionPane.showInputDialog("What Width?");
                int widthRect = Integer.parseInt(whatWidth);                 
            }
     
         areaTriangle (triHeight, triBase, triOutput, shapeNum);  
         areaCircle (circOutput, circRadi, shapeNum);
         areaRectangle (rectOutput, lengthRect, widthRect, shapeNum);
     
     
        }
        public static void areaTriangle(double triBase,double triHeight, double triOutput, int shapeNum) {
            triOutput = (triBase*triHeight)/2;
              if (shapeNum == 1 ){
             System.out.println(triOutput);
                  }
                }
        public static void areaCircle (double circOutput,double circRadi, int shapeNum )
          {
           circOutput = (Math.pow (circRadi, 2)*Math.PI);    
                 if(shapeNum == 2 )
            {
            System.out.println(circOutput);
            }
     
           }
     
        public static void areaRectangle(int rectOutput, int lengthRect, int widthRect, int shapeNum) {
            rectOutput = lengthRect*widthRect;
              if(shapeNum == 3 ) {
            System.out.println(rectOutput);
      }  
         }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Errors with beginning jOptionPane code

    they are getting errors.
    When you get errors, you need to copy the full text of the error messages and paste it here so we can see what the errors are.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors with beginning jOptionPane code

    Getting cannot find symbol errors in main:
    areaTriangle (triHeight, triBase, triOutput, shapeNum);  
         areaCircle (circOutput, circRadi, shapeNum);
         areaRectangle (rectOutput, lengthRect, widthRect, shapeNum);

    When running the program:

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at area.main(area.java:53)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Errors with beginning jOptionPane code

    cannot find symbol errors
    You need to post the full text of the compiler's error message that shows the line where the error occurred and the symbol that is not found.

    Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    This shows the source line and the symbol.

    The compiler is saying that the code is using symbols that are not defined. Look at the source statement with the error and determine what variable needs a definition.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors with beginning jOptionPane code

    The only error I am getting from compiler is
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
    at area.main(area.java:53)
    Java Result: 1

    I am using NetBeans.

    Off to the side it's saying:
    Cannot find symbol, triHeight, triBase, circRadi, lengthRect, widthRect.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Errors with beginning jOptionPane code

    That's a problem with some IDEs. The don't give you error messages that can be copied to a forum where you are asking for help. Until you can get a listing of the compiler's error mesages, you'll have to wait until someone will copy your code and put it in their compiler. The problem with that approach is that you never will be able to get help without that person copying your code to his compiler.

    Cannot find symbol, triHeight, triBase, circRadi, lengthRect, widthRect.
    Where are those variables defined? Are their definitions in scope with where they are being referencecd?(the definition is inside the same pair of {}s as where they are being used). Look up the definition of scope for more info

    Move the variable definitions up to some levels to where they can be seen where you are using them.
    Last edited by Norm; September 28th, 2012 at 09:37 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors with beginning jOptionPane code

    Variables are defined below
    if (shapeNum == 1) {
                String whatBase = JOptionPane.showInputDialog("Triangle Base?");
                double triBase = Double.parseDouble(whatBase);
                String whatHeight = JOptionPane.showInputDialog("Triangle Height?");
                double triHeight = Double.parseDouble(whatHeight);
     
             }
           if (shapeNum == 2) {
     
                String whatRadi = JOptionPane.showInputDialog("What Radius?");
                double circRadi = Double.parseDouble(whatRadi);
            }
           if (shapeNum == 3) {
                System.out.println ("Length of rectangle?");
                String whatLength = JOptionPane.showInputDialog("What Length?");
                int lengthRect = Integer.parseInt(whatLength);
                String whatWidth = JOptionPane.showInputDialog("What Width?");
                int widthRect = Integer.parseInt(whatWidth);

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors with beginning jOptionPane code

    As far as scope, we haven't learned about that in class yet.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Errors with beginning jOptionPane code

    your problem is with the scope of the definitions of the variables. The definition is NOT known outside of the enclosing {}s.
    {  //  begin scope for x
       int x = 20;
    }  // end scope for x
    // x is  not known here
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors with beginning jOptionPane code

    Any insight on going about fixing this? If statements make this hard.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Errors with beginning jOptionPane code

    Move the definitions of the variables so they are in scope. That usually means putting the definitions within the same set of {}s.
    The first set of {}s is for a class.
    The next set of {}s is for a method (and others like static blocks)
    Within methods there are {}s used by if and while statements for example.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    LoganC (September 28th, 2012)

  13. #12
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Errors with beginning jOptionPane code

    Thanks Norm.

  14. #13
    Junior Member
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Errors with beginning jOptionPane code

    I would suggest putting the area outputs in the initial if statements with asking fr the base that might make it easier to look at.

Similar Threads

  1. Whats Wrong? Beginning Code!
    By LoganC in forum What's Wrong With My Code?
    Replies: 19
    Last Post: September 21st, 2012, 08:09 PM
  2. weird errors in my code
    By jobje325 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 17th, 2012, 11:15 AM
  3. Getting Exception Errors, But Not Sure How To Fix Code
    By noel222 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2012, 12:09 PM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. Please check the code for the errors
    By nrao in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 16th, 2010, 05:37 PM