Solved

calling a method in Java

Posted on 2014-02-20
6
341 Views
Last Modified: 2014-02-20
I'm using Eclipse IDE for Java Developer.

So in my java main method I set a variable like this:


sum = number1 + number2;

Then  I use this which displays my sum in a window.

// display the results
JOptionPane.showMessageDialog( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE );

So this works correctly.



Now I'm trying to do something similar but I want to have the sum calculation in a method called sumNumbers.  So if my method looks like this how would I call this method using JOptionPane

public void sumNumbers(double number1, double number2) {
      double sum;
            
      sum = number1 + number2;
            
}


I tried something like this, but i don't think it's correct:

// display the results
JOptionPane.showMessageDialog( null, "The sum is " + sumNumbers(number1, number2), "Results", JOptionPane.PLAIN_MESSAGE );


Any know how I call a method with parameters using JOptionPane.showMessageDialog?
0
Comment
Question by:maqskywalker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 300 total points
ID: 39872789
>> "The sum is " + sumNumbers(number1, number2)
If you want to use it like this, your method should return the result:

public double sumNumbers(double number1, double number2) {
       return number1 + number2;
}
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 200 total points
ID: 39872808
Further to zzynx correct answer, the void keyword in your method signifies that the method does not return any value. As you can see in zzynx's response the method specifies that it returns a double value.
 
Have a look at this tutorial for more information
http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
0
 
LVL 1

Author Comment

by:maqskywalker
ID: 39872851
My question is on calling a method using JOptionPane.showMessageDialog

So then assuming my method looks like this:

public double sumNumbers(double number1, double number2) {
       return number1 + number2;
}




Is this how I would call it ?

JOptionPane.showMessageDialog( null, "The sum is " + sumNumbers(number1, number2), "Results", JOptionPane.PLAIN_MESSAGE );
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 39872854
Yes!
Just try it.
0
 
LVL 1

Author Comment

by:maqskywalker
ID: 39872860
thanks guys. your help worked.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 39872928
You're welcome.
Thanx 4 axxepting
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question