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 3 of 3

Thread: when i run applet

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default when i run applet

    when i run applet my radius is 0 why? heres my code



    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;

    public class Assign3 extends JApplet
    {
    private double diameter;
    private double circum;
    private double area;
    private double pi = Math.PI;
    private double radius;

    public void init()
    {
    String radius = JOptionPane.showInputDialog(
    "Enter the radius of the circle" );

    double radius1 = Double.parseDouble( radius );
    radius = radius;
    diameter = 2*radius1;
    circum = 2*(pi*radius1);
    area = pi*(radius1*radius1);
    }
    public void paint( Graphics g )
    {
    super.paint( g );
    g.drawString( "The radius is " + radius , 25, 25);
    g.drawString( "The diameter is " + diameter , 25, 45);
    g.drawString( "The circumference is " + circum , 25, 65);
    g.drawString( "The area is " + area , 25, 85);
    }
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: when i run applet

    It is good practice to not declare variables with the same name as a variable with a larger scope. Consider the code below:

    private double radius;
    ...
    {
    String radius = JOptionPane.showInputDialog(
    "Enter the radius of the circle" );
     
    double radius1 = Double.parseDouble( radius );
    radius = radius;
    ...
    }

    Notice how you have a double named radius and a String named radius? Well, when you make the statement radius=radius, it is looking at the variable with the most relevant scope, which means it is looking at the radius variable that is a String. So, you are setting the String radius to be equal to the String radius.

    You also probably meant to set radius equal to radius1.

    So, to fix this problem you have two options.
    1) Since your double radius is a class variable, you can use the this.variable declaration, as shown below:

    private double radius;
    ...
    {
    String radius = JOptionPane.showInputDialog(
    "Enter the radius of the circle" );
     
    double radius1 = Double.parseDouble( radius );
    this.radius = radius1;
    ...
    }

    2) You can change the name of the variable with the least scope, like shown below:
    private double radius;
    ...
    {
    String radiusString = JOptionPane.showInputDialog(
    "Enter the radius of the circle" );
     
    double radius1 = Double.parseDouble( radiusString );
    radius = radius1;
    ...
    }
    Last edited by aussiemcgr; February 22nd, 2011 at 04:54 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: when i run applet

    ahh i see this i have radius set in string and my variable therefore it goes to both.. thanks

Similar Threads

  1. applet img
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: April 7th, 2010, 09:14 PM
  2. applet menubar
    By tabutcher in forum Java Applets
    Replies: 2
    Last Post: March 5th, 2010, 09:30 AM
  3. Deploying an applet with jsp
    By IamKira in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: January 27th, 2010, 08:28 AM
  4. applet fail
    By wolfgar in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 21st, 2010, 06:24 AM
  5. [SOLVED] applet vs. gui app
    By rptech in forum AWT / Java Swing
    Replies: 3
    Last Post: August 27th, 2009, 09:13 AM