[Beginner] Where am i wrong?

P

pmusu

threeInt.java:51: incompatible types
found : int
required: java.lang.String
sum = (number1 + number2 + number3);
^
threeInt.java:54: incompatible types
found : int
required: java.lang.String
average = (number1 + number2 + number3) / 3;
^
threeInt.java:57: incompatible types
found : int
required: java.lang.String
product = (number1 * number2 * number3);
^
3 errors

Tool completed with exit code 1

Here is code:

/* 2.18 Write an application that inputs three integers from the user
and displays the sum, average
, product, smallest and largest of the information in an information
dialog. Note the average
should be in integer. */

// Java Packages
import javax.swing.JOptionPane;


public class threeInt {

// main method begins execution of Java application
public static void main( String args[] )
{
String firstNumber; // first string entered by user
String secondNumber; // second string entered by user
String thirdNumber; // third string entered by user


String sum;
String average;
String product;
String smallest;
String largest;

int number1;
int number2;
int number3;

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

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

// read 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 );

// initialise results to empty String
sum = "";
average = "";
product = "";
smallest = "";
largest = "";

// compute sum
sum = (number1 + number2 + number3);

// compute average
average = (number1 + number2 + number3) / 3;

// compute product
product = (number1 * number2 * number3);

// compute smallest
if (number1 < number2);
if (number1 < number3);
smallest = smallest + "Smallest number is" + number1;

if (number2 < number1);
if (number2 < number3);
smallest = smallest + "Smallest number is" + number2;

if (number3 < number2);
if (number3 < number1);
smallest = smallest + "Smallest number is" + number3;


// compute largest
if (number1 > number2);
if (number1 > number3);
largest = largest + "Largest number is" + number1;

if (number2 > number1);
if (number2 > number3);
largest = largest + "Largest number is" + number2;

if (number3 > number2);
if (number3 > number1);
largest = largest + "Largest number is" + number3;

// Display sum
JOptionPane.showMessageDialog( null, sum, "Sum",
JOptionPane.INFORMATION_MESSAGE );

// Display average
JOptionPane.showMessageDialog( null, average, "Average",
JOptionPane.INFORMATION_MESSAGE );

// Display product
JOptionPane.showMessageDialog( null, product, "Product",
JOptionPane.INFORMATION_MESSAGE );

// Display smallest
JOptionPane.showMessageDialog( null, smallest, "Smallest",
JOptionPane.INFORMATION_MESSAGE );

// Display largest
JOptionPane.showMessageDialog( null, largest, "Largest",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 ); // terminate application

} // end method main

} // end class threeInt
 
K

klynn47

The problem is the output of the arithmetic expression is an int, and
you can't directly assign an int to a String. You can either use
String.valueOf or concatenate the output of the expression with an
empty String.
 
A

anonymous

pmusu said:
threeInt.java:51: incompatible types
found : int
required: java.lang.String
sum = (number1 + number2 + number3);
^
threeInt.java:54: incompatible types
found : int
required: java.lang.String
average = (number1 + number2 + number3) / 3;
^
threeInt.java:57: incompatible types
found : int
required: java.lang.String
product = (number1 * number2 * number3);
^
3 errors

Tool completed with exit code 1

Here is code:

/* 2.18 Write an application that inputs three integers from the user
and displays the sum, average
, product, smallest and largest of the information in an information
dialog. Note the average
should be in integer. */

// Java Packages
import javax.swing.JOptionPane;


public class threeInt {

// main method begins execution of Java application
public static void main( String args[] )
{
String firstNumber; // first string entered by user
String secondNumber; // second string entered by user
String thirdNumber; // third string entered by user


String sum;
String average;
String product;
String smallest;
String largest;

int number1;
int number2;
int number3;

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

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

// read 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 );

// initialise results to empty String
sum = "";
average = "";
product = "";
smallest = "";
largest = "";

// compute sum
sum = (number1 + number2 + number3);

// compute average
average = (number1 + number2 + number3) / 3;

// compute product
product = (number1 * number2 * number3);

// compute smallest
if (number1 < number2);
if (number1 < number3);
smallest = smallest + "Smallest number is" + number1;

if (number2 < number1);
if (number2 < number3);
smallest = smallest + "Smallest number is" + number2;

if (number3 < number2);
if (number3 < number1);
smallest = smallest + "Smallest number is" + number3;


// compute largest
if (number1 > number2);
if (number1 > number3);
largest = largest + "Largest number is" + number1;

if (number2 > number1);
if (number2 > number3);
largest = largest + "Largest number is" + number2;

if (number3 > number2);
if (number3 > number1);
largest = largest + "Largest number is" + number3;

// Display sum
JOptionPane.showMessageDialog( null, sum, "Sum",
JOptionPane.INFORMATION_MESSAGE );

// Display average
JOptionPane.showMessageDialog( null, average, "Average",
JOptionPane.INFORMATION_MESSAGE );

// Display product
JOptionPane.showMessageDialog( null, product, "Product",
JOptionPane.INFORMATION_MESSAGE );

// Display smallest
JOptionPane.showMessageDialog( null, smallest, "Smallest",
JOptionPane.INFORMATION_MESSAGE );

// Display largest
JOptionPane.showMessageDialog( null, largest, "Largest",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 ); // terminate application

} // end method main

} // end class threeInt
Where is that 'solve_my_homework' newsgroup again?

Look at your variables, they're String types. Doesn't make sense to try
and do arithmetic. What your're attempting is this:

"My Homework" = ("not" * "someone" * "elses" * "problem") / 3;

sum = "";
 
E

Emma Kilkelly via JavaKB.com

Just change all your variables from String to int and you should be fine
 
B

Bob

Emma said:
Just change all your variables from String to int and you should be fine

He won't be fine if this is his homework. (And it appears that it is.)

Cheating like this will mean he's the kinda guy who doesn't do any of
the recommended reading for the course. Which means he'll score low on
the final exam/s for the course. Which means he'll end up with a 2:2
like me, and be unemployable.

Unless he cheats on job applications, too.
 
T

TechBookReport

pmusu said:
threeInt.java:51: incompatible types
found : int
required: java.lang.String
sum = (number1 + number2 + number3);
^
threeInt.java:54: incompatible types
found : int
required: java.lang.String
average = (number1 + number2 + number3) / 3;
^
threeInt.java:57: incompatible types
found : int
required: java.lang.String
product = (number1 * number2 * number3);
^
3 errors

Tool completed with exit code 1

Here is code:

/* 2.18 Write an application that inputs three integers from the user
and displays the sum, average
, product, smallest and largest of the information in an information
dialog. Note the average
should be in integer. */

// Java Packages
import javax.swing.JOptionPane;


public class threeInt {

// main method begins execution of Java application
public static void main( String args[] )
{
String firstNumber; // first string entered by user
String secondNumber; // second string entered by user
String thirdNumber; // third string entered by user


String sum;
String average;
String product;
String smallest;
String largest;

int number1;
int number2;
int number3;

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

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

// read 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 );

// initialise results to empty String
sum = "";
average = "";
product = "";
smallest = "";
largest = "";

// compute sum
sum = (number1 + number2 + number3);

// compute average
average = (number1 + number2 + number3) / 3;

// compute product
product = (number1 * number2 * number3);

// compute smallest
if (number1 < number2);
if (number1 < number3);
smallest = smallest + "Smallest number is" + number1;

if (number2 < number1);
if (number2 < number3);
smallest = smallest + "Smallest number is" + number2;

if (number3 < number2);
if (number3 < number1);
smallest = smallest + "Smallest number is" + number3;


// compute largest
if (number1 > number2);
if (number1 > number3);
largest = largest + "Largest number is" + number1;

if (number2 > number1);
if (number2 > number3);
largest = largest + "Largest number is" + number2;

if (number3 > number2);
if (number3 > number1);
largest = largest + "Largest number is" + number3;

// Display sum
JOptionPane.showMessageDialog( null, sum, "Sum",
JOptionPane.INFORMATION_MESSAGE );

// Display average
JOptionPane.showMessageDialog( null, average, "Average",
JOptionPane.INFORMATION_MESSAGE );

// Display product
JOptionPane.showMessageDialog( null, product, "Product",
JOptionPane.INFORMATION_MESSAGE );

// Display smallest
JOptionPane.showMessageDialog( null, smallest, "Smallest",
JOptionPane.INFORMATION_MESSAGE );

// Display largest
JOptionPane.showMessageDialog( null, largest, "Largest",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 ); // terminate application

} // end method main

} // end class threeInt
You are defining sum and average etc as strings, but the results of your
calculations are ints.

Pan
======================================================================
TechBookReport http://www.techbookreport.com
 
P

pmusu

Sorry, but this is not homework,

consider it as a self teaching course.

And as far as i know i am not breaking any NG rules since i TRIED
working
the question.

It is not like i am telling you, work this question for me.

Thanks
 
P

pmusu

Here is the modified answer. I added a temp integer. However i still
have to sort the largest and smallest, since the answers are not
correct.

/* 2.18 Write an application that inputs three integers from the user
and displays the sum, average
, product, smallest and largest of the information in an information
dialog. Note the average
should be in integer. */

// Java Packages
import javax.swing.JOptionPane;


public class threeInt {

// main method begins execution of Java application
public static void main( String args[] )
{
String firstNumber; // first string entered by user
String secondNumber; // second string entered by user
String thirdNumber; // third string entered by user


String sum;
String average;
String product;
String smallest;
String largest;

int number1;
int number2;
int number3;
int temp;

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

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

// read 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 );

// initialise results to empty String
sum = "";
average = "";
product = "";
smallest = "";
largest = "";

// compute sum
temp = (number1 + number2 + number3);
sum = sum + temp;

// compute average
temp = 0;
temp = (number1 + number2 + number3) / 3;
average = average + temp;

// compute product
temp = 0;
temp = (number1 * number2 * number3);
product = product + temp;

// compute smallest
if (number1 < number2);
if (number1 < number3);
smallest = smallest + number1;

if (number2 < number1);
if (number2 < number3);
smallest = smallest + number2;

if (number3 < number2);
if (number3 < number1);
smallest = smallest + number3;


// compute largest
if (number1 > number2);
if (number1 > number3);
largest = largest + number1;

if (number2 > number1);
if (number2 > number3);
largest = largest + number2;

if (number3 > number2);
if (number3 > number1);
largest = largest + number3;

// Display sum
JOptionPane.showMessageDialog( null, sum, "Sum",
JOptionPane.INFORMATION_MESSAGE );

// Display average
JOptionPane.showMessageDialog( null, average, "Average",
JOptionPane.INFORMATION_MESSAGE );

// Display product
JOptionPane.showMessageDialog( null, product, "Product",
JOptionPane.INFORMATION_MESSAGE );

// Display smallest
JOptionPane.showMessageDialog( null, smallest, "Smallest",
JOptionPane.INFORMATION_MESSAGE );

// Display largest
JOptionPane.showMessageDialog( null, largest, "Largest",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 ); // terminate application

} // end method main

} // end class threeInt
 
L

Larry Coon

pmusu said:
String sum;
String average;
String product;
String smallest;
String largest;

Why would you think that any of these should be Strings?
Use appropriate data types for all your variables -- most
of your problems will go away.


Larry Coon
University of California
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top