Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number calculations

 
Derek Harper
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Renato Losio
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'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
 
Derek Harper
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I found that error right after I submitted the post.
 
Derek Harper
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"result" is not declared in your code, and there is no method called JOptionPane.showMessageDialog that has 5 parameters.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"result" is not declared in your code

In fact, it is declared, but at the wrong place
You should declare it before using it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic