This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Transfer Learning for Natural Language Processing (MEAP) and have Paul Azunre on-line!
See this thread for details.
Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Number calculations

 
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



 
Sheriff
Posts: 11343
Mac Safari Java
  • 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.)
 
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 ]
 
Rancher
Posts: 43015
76
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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.
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic