![]() |
[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: ------- |
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... |
Okay I changed the variables to doubles and I got a clean compile too. Now what do I have to do in the "display" section to get all three results to appear in a left-alligned column?
|
Put your message in a JLabel and pass that to JOptionPane.showMessageDialog(), instead of passing the string.
Code:
// display result |
ok, thanks for the help. I appreciate it
|
All times are GMT -8. The time now is 05:45 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project