Search code examples
javaswingmethodsjoptionpane

Beginner Programming in Java - using an integer in a Dialog (JOptionPane) box from another method


I have just started using Java and I am trying to write some code which will take some Inputs using Dialogue boxes and then transform/manipulate those Inputs further

I can get the desired answer if I only use one method but I am trying to use multiple methods. I believe I am incorrectly trying to retrieve values from other methods but I am not sure where I am going wrong. here is what I have written so far - the objective is to take the input numbers and swap them and then add 100 and 50 to the swapped numbers and display in Dialogue boxes.

I believe the error is with the fourth method because when I try to refer to number1 or number2 in the

My code is as follows:

import javax.swing.JOptionPane; 

public class NumberSwap
{

   public static void main(String[] args)
   {
      greeting();
      getFirstNumber();
      getSecondNumber();
      swapNumber(); 
   } 

   public static void greeting()
   {
   // display a dialog box with a message and custom Title
   JOptionPane.showMessageDialog(null,"Welcome to my program!", "Welcome!", JOptionPane.PLAIN_MESSAGE);
   } 

   public static int getFirstNumber()
   {
   // obtain user input from JOptionPane input dialogs
   String firstNumber = JOptionPane.showInputDialog(null, "Please enter a number", "First number?", JOptionPane.QUESTION_MESSAGE);

   // convert String inputs to int values
   int number1 = Integer.parseInt(firstNumber);
   return number1;
   } 

   public static int getSecondNumber()
   {
   // obtain user input from JOptionPane input dialogs
   String secondNumber = JOptionPane.showInputDialog(null, "Please enter a number", "Second number?", JOptionPane.QUESTION_MESSAGE);

   // convert String inputs to int values
   int number2 = Integer.parseInt(secondNumber);
   return number2;
   } 

    public static void swapNumber()
   {
   // swap number1 and number2 in order and increment  the new first number by 100 and the new second number by 50   
   int swap1 = number2+100;
   int swap2 = number1+50;

   JOptionPane.showMessageDialog(null, "new value of first number is " +swap1, "Summary", JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, "new value of second number is " +swap2, "Summary", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
   }  

}// end class NumberSwap

Solution

  • In your posted code, you call methods, but you don't use their returned values. You also need to "pass" the returned values to swapNumber.

    You should use something like this:

    int num1 = getFirstNumber();
    int num2 = getSecondNumber();
    swapNumber(num1, num2);
    

    with

    public static void swapNumber(int number1, int number2)
    {
       // swap number1 and number2 in order and increment  the new first number by 100 and the new second number by 50   
       int swap1 = number2+100;
       int swap2 = number1+50;
       // etc
    

    Some related reading (Oracle tutorial):