0

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

2 Answers2

3

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):

  • Thank you for pointing that out, I had tried calling the getFirstNumber and getSecondNumber in the swapNumber method but as you have noted the returned result wasn't referenced so really all I was doing was calling those two methods again. – Aleksander Jun 03 '15 at 21:52
0

As alluded to above, you're not keeping your variables when they return from the methods getFirstNumber and getSecondNumber. Since you want to keep a value and modify more than one in a later function, its a great time to use class variables. Something like this may be more elegant.

public class NumSwap{

    private int num1;
    private int num2;

    public static void main(String[] args)
       {
          greeting();
          this.num1 = getNumber("First number?");
          this.num2 = getNumber("Second Number?");
          swapNumbers();
          System.exit(0);
       }

    public static int getNumber( String prompt )
       {
       // obtain user input from JOptionPane input dialogs
       String numberStr = JOptionPane.showInputDialog(null, "Please enter a number", prompt, JOptionPane.QUESTION_MESSAGE);

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

    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 = this.num2+100;
       int swap2 = this.num1+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);
       } 
}

My apologies if you're not yet familiar with class variables. In your particular case they might not be necessary, but they become so in the future. In Java ( and many other languages ) variables are passed by value, usually meaning a copy is made, and then that copy is edited. Since that wasn't the question, I'll leave this here if you'd like to know more Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
Rabbit
  • 131
  • 2