1

I'm a newbie to Java and I want to pass some outputs to outside of the current class in Java. First, I asked user to put some numbers and stored as string. Then converted those into integers. What I want to do is turning those values into some methods. Here is my code.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame {
    JTextField x = new JTextField("X",5);
    JTextField y = new JTextField("Y",5);
    JTextField z = new JTextField("Z",5);
    JButton chk = new JButton("Check");
    public Main(){
        JPanel newPanel = new JPanel();
        newPanel.add(x);
        newPanel.add(y);
        newPanel.add(z);
        newPanel.add(chk);
            chk.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //These are the values I want to pass
                    int xi = Integer.parseInt(x.getText());
                    int yi = Integer.parseInt(y.getText());
                    int zi = Integer.parseInt(z.getText());
                    System.out.printf("you data is %d\n",xi);
                    System.out.printf("Your data is %d\n",yi);
                    System.out.printf("you data is %d",zi);
                }
            });
        add(newPanel);
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[]args) {
        new Main().setVisible(true);
    }
}

As you see, I want to pass those outputs (namely xi, yi and zi) into another class. Like following...

int result = xi+yi+zi;
System.out.println(result);

How can I do that? How to pass those data into another class? Thanks

0

4 Answers 4

2

There are many ways to do that and it totally depend on the use case. If you have only three variables then you can pass them as it in the method argument.

i.e I have my other class.

class OtherClass {
   static void doSomething(int x, int y, int z) {
        //do something
   }
}

or

You can use constructor to initialise instance variable to OtherClass i.e

class OtherClass {
   int a;
   int b;
   int c;
   
   OtherClass(int x, int y, int z) {
       this.a = x;
       this.b = y;
       this.c = z;
   }
}

//calling
OtherClass o = new OtherClass(1,2,3);
Sign up to request clarification or add additional context in comments.

Comments

1

Just create a class and a method. Place your code i.e. whatever action you want to perform inside that method by making the method as void or any other return type.

for example in your program you want to print the sum of xi, yi, and zi

public class MyClass {
    public void sum(int xi, int yi, int zi) {
        System.out.println(xi+yi+zi);
    }
    
//  or
    
    public int sum1(int xi, int yi, int zi) {
        return xi+yi+zi;
    }
    
}

Create an object of this class and call the method. Based on your code you can do like this

 @Override
     public void actionPerformed(ActionEvent e) {
         //storing the values in the ""global"" variables
         xi = Integer.parseInt(x.getText());
         yi = Integer.parseInt(y.getText());
         zi = Integer.parseInt(z.getText());
         MyClass myClass = new MyClass();
         myClass.sum(xi, yi, zi);
        
//      or
        
         int result = myClass.sum1(xi, yi, zi);
         System.out.println(result);
     }

1 Comment

Kudos bro, you rock.
1

How about creating 3 local variables in main, which you will be able to use in the entire function? As I see the variables that you declared are only usable in the event function.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame {
    JTextField x = new JTextField("X",5);
    JTextField y = new JTextField("Y",5);
    JTextField z = new JTextField("Z",5);
    int xi,yi,zi; //new variables to use later on
    JButton chk = new JButton("Check");
    public Main(){
        JPanel newPanel = new JPanel();
        newPanel.add(x);
        newPanel.add(y);
        newPanel.add(z);
        newPanel.add(chk);
            chk.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //storing the values in the ""global"" variables
                    xi = Integer.parseInt(x.getText());
                    yi = Integer.parseInt(y.getText());
                    zi = Integer.parseInt(z.getText());
                    System.out.printf("you data is %d\n",xi);
                    System.out.printf("Your data is %d\n",yi);
                    System.out.printf("you data is %d",zi);
                }
            });
        add(newPanel);
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[]args) {
        new Main().setVisible(true);
    }
}

Then if you want to use them later like when another button is pressed you will be able to do it.

Hope this is what you were asking for... :)

Comments

1

You can add a method to the Main class which returns an array with said numbers.

public int[] func(){
     int[] arr = new int[3];
     for(...) //Input the numbers into the array (ideally you would have the Jtext fields in an array you can loop over.
     return arr;

Hope it helped you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.