File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Number calculations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Number calculations" Watch "Number calculations" New topic
Author

Number calculations

Derek Harper
Greenhorn

Joined: Aug 02, 2006
Posts: 25
What is wrong with the following program?


--------------------------------------------


import javax.swing.JOptionPane;

public class Calculate2 {

public static void main(String[] args) {

String firstNumber; // first string entered by user
String secondNumber; // second number entered by user
String thirdNumber; // third number entered by user

int number1; // first number
int number2; // second number
int number3; third number

int average; // average of the numbers
int largest; // largest number
int product; // product of the numbers
int smallest; // smallest number
int sum; // sum of the numbers

// read in first number from user as a string
firstNumber = JOptionPane.showInputDialog( "Enter first integer");

// read in second number from user as a string
secondNumber = JOptionPane.showInputDialog( "Enter second integer" );

// read in third number from user as a string
thirdNumber = JOptionPane.showInputDialog( "Enter third integer" );

// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
number3 = Integer.parseInt( thirdNumber );

// initialize largest and smallest
largest = number1;
smallest = number2;

// determine smallest and largest
if (number1 > number2)
result = "The largest is " + number1;
else if ( number2 < number1)
result = "The smallest is " + number2;

// perform calculations
sum = number1 + number2 + number3;
product = number1 * number2 * number3;
average = ( number1 + number2 + number3 ) / 3;

// create result string
String result;
if ( sum = number1 + number2 + number3)
result = "The sum is + sum" ;
if ( product = number1 * number2 * number3)
result = "The product is + product";
if ( average = (number1 + number2 + number3) / 3)
result = "The average is + average";

// display results
JOptionPane.showMessageDialog( null, result, "Calculation Results", "The largest is " + largest + "\nThe smallest is " + smallest + "\nThe sum is " + sum + "\nThe product is " + product + "\nThe average is " + average, "Results", JOptionPane.PLAIN_MESSAGE );
}
}


---------------------------------------------------

When I compile the program, I receive the following error message:


Calculate2.java:16: ';' expected
^
1 error



marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The compiler is telling you there's something wrong on (or around) line 16. Do you see the problem with the line in bold...?

int number1; // first number
int number2; // second number
int number3; third number

(There are other errors after that, but this will get you past the first obstacle.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Renato Losio
Ranch Hand

Joined: Nov 23, 2005
Posts: 99
'third number is not a comment.

You have:

int number3; third number

It should be:

int number3; // third number

Note: you can quickly fix those problems using a IDE :-)

Cheers.

Renato


Renato Losio - www.arsenio.it - renatoweb@arsenio.it
Derek Harper
Greenhorn

Joined: Aug 02, 2006
Posts: 25
Thanks. I found that error right after I submitted the post.
Derek Harper
Greenhorn

Joined: Aug 02, 2006
Posts: 25
I made three corrections.

-------------------------------------------

import javax.swing.JOptionPane;

public class Calculate2 {

public static void main(String[] args) {

String firstNumber; // first string entered by user
String secondNumber; // second number entered by user
String thirdNumber; // third number entered by user

int number1; // first number
int number2; // second number
int number3; // third number

int average; // average of the numbers
int largest; // largest number
int product; // product of the numbers
int smallest; // smallest number
int sum; // sum of the numbers

// read in first number from user as a string
firstNumber = JOptionPane.showInputDialog( "Enter first integer" );

// read in second number from user as a string
secondNumber = JOptionPane.showInputDialog( "Enter second integer" );

// read in third number from user as a string
thirdNumber = JOptionPane.showInputDialog( "Enter third integer" );

// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
number3 = Integer.parseInt( thirdNumber );

// initialize largest and smallest
largest = number1;
smallest = number2;

// determine smallest and largest
if (number1 > number2)
result = "The largest is " + number1;
else if ( number2 < number1)
result = "The smallest is " + number2;

// perform calculations
sum = number1 + number2 + number3;
product = number1 * number2 * number3;
average = ( number1 + number2 + number3 ) / 3;

// create result string
String result;

if ( sum == number1 + number2 + number3)
result = "The sum is + sum" ;
if ( product == number1 * number2 * number3)
result = "The product is + product";
if ( average == (number1 + number2 + number3) / 3)
result = "The average is + average";

// display results
JOptionPane.showMessageDialog( null, result, "Calculation Results", "The largest is " + largest +
"\nThe smallest is " + smallest + "\nThe sum is " + sum + "\nThe product is " + product +

"\nThe average is " + average, "Results", JOptionPane.PLAIN_MESSAGE );
}
}
-------------------------------------------------

The error that i'm getting now is as follows:



Calculate2.java:43: cannot find symbol
symbol : variable result
location: class Calculate2
result = "The largest is " + number1;
^
Calculate2.java:45: cannot find symbol
symbol : variable result
location: class Calculate2
result = "The smallest is " + number2;
^
Calculate2.java:63: cannot find symbol
symbol : method showMessageDialog(<nulltype>,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int)
location: class javax.swing.JOptionPane
JOptionPane.showMessageDialog( null, result, "Calculation Results", "The largest is " + largest +
^
3 errors



I know it's something rather simple, but i'm stuck on this one.

[ September 24, 2006: Message edited by: Derek Harper ]

[ September 24, 2006: Message edited by: Derek Harper ]
[ September 24, 2006: Message edited by: Derek Harper ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
"result" is not declared in your code, and there is no method called JOptionPane.showMessageDialog that has 5 parameters.


Ping & DNS - my free Android networking tools app
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

"result" is not declared in your code

In fact, it is declared, but at the wrong place
You should declare it before using it.


[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number calculations