01-15-2007, 09:09 AM | #1 (permalink) |
Tilted
|
[Java]Help my program plz
Right now I am writing a simple java program where a user inputs the radius of a circle and the program, in turn, calculates and displays the diameter, circumference, and area of the circle in a simple message box. nothing about this program is complicated, but this is my first real program and I am having a problem getting it to compile. I keep receiving this error...
Circle.java:7: ';' expected ^ 1 error I have reviewed my code thoroughly and can't find any instance where I am missing a semi-colon. here is my code, please. // Java Programming Assignment 2.1 // Program to calculate circular circumference, diameter, and area //Java Packages import javax.swing.JOptionPane; // use JOptionPane public class Circle { // main method begins execution of Java application public static void main( String args[] ) { String firstNumber; // first string entered by user int radius; // radius int diameter; // radius * 2 int area; // Pi * radius^2 int circumference; // 2 * Pi * radius int pi; // read first number from user as a String firstNumber = JOptionPane.showInputDialog( "Enter radius of a circle" ); // pi value constant pi = 3.14159; // convert radius from type String to typ int radius = Integer.parseInt( firstNumber ); // calculate diameter diameter = radius * 2; // calculate area area= pi * radius * radius; // calculate circumference circumference = 2 * pi * radius; // display result JOptionPane.showMessageDialog( null, "The Diameter is: " + diameter ); System.exit( 0 ); // terminate application with window } // end method main } // end class Circle It is currently set up to calculate all three results, but only to display the diameter. Also, if you can, please explain how i can get my 3 results to display like this... Diameter: ------- Circumference: ------- Area: -------
__________________
For Sale. |
01-15-2007, 09:34 AM | #2 (permalink) |
Upright
|
You're not missing a semi-colon, but the compiler does complain about a possible loss of precision when you use int instead of double for pi, radius, diameter and friends.
After making that change, I got a clean compile on JDK 1.6.0: Code:
Parsing Input... Compiling... Standard Output from javac: [search path for source files: [.]] [search path for class files: [/usr/local/java/jdk1.6.0/jre/lib/resources.jar, /usr/local/java/jdk1.6.0/jre/lib/rt.jar, /usr/local/java/jdk1.6.0/jre/lib/sunrsasign.jar, /usr/local/java/jdk1.6.0/jre/lib/jsse.jar, /usr/local/java/jdk1.6.0/jre/lib/jce.jar, /usr/local/java/jdk1.6.0/jre/lib/charsets.jar, /usr/local/java/jdk1.6.0/jre/classes, /usr/local/java/jdk1.6.0/jre/lib/ext/dnsns.jar, /usr/local/java/jdk1.6.0/jre/lib/ext/sunpkcs11.jar, /usr/local/java/jdk1.6.0/jre/lib/ext/sunjce_provider.jar, /usr/local/java/jdk1.6.0/jre/lib/ext/localedata.jar, .]] [parsing started /tmp/15602/Circle.java] [parsing completed 145ms] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/javax/swing/JOptionPane.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/String.class)] [checking Circle] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/awt/Component.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/javax/swing/JComponent.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/awt/Container.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Integer.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Number.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/System.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Error.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/awt/HeadlessException.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/UnsupportedOperationException.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/RuntimeException.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Exception.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Throwable.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/NumberFormatException.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/IllegalArgumentException.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/StringBuilder.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/AbstractStringBuilder.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/CharSequence.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/io/Serializable.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Comparable.class)] [loading /usr/local/java/jdk1.6.0/lib/ct.sym(META-INF/sym/rt.jar/java/lang/StringBuffer.class)] [wrote /tmp/15602/Circle.class] [total 1705ms] Output File(s) Circle.class |
01-15-2007, 10:30 AM | #4 (permalink) |
Upright
|
Put your message in a JLabel and pass that to JOptionPane.showMessageDialog(), instead of passing the string.
Code:
// display result JLabel message = new JLabel("The diameter is: " + diameter, JLabel.LEADING); JOptionPane.showMessageDialog( null, message ); |
Tags |
javahelp, plz, program |
|
|