# Number calculations

Derek Harper

Greenhorn

Posts: 25

posted 9 years ago

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

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

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

posted 9 years ago

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

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

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

Posts: 99

posted 9 years ago

'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

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

Posts: 25

Derek Harper

Greenhorn

Posts: 25

posted 9 years ago

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

result = "The sum is + sum" ;

if ( product

result = "The product is + product";

if ( average

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 ]

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

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: 42968

73