?
Solved

calling a method in Java

Posted on 2014-02-20
6
Medium Priority
?
343 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 1200 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:
Michael Fowler earned 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

762 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