Excali Excali - 1 month ago 8
Java Question

How can I transfer values of an int and string from one program to another in Java

Ok so In my code I'm asking the user for their name and asking them to click one of 3 buttons which gives a variable a corresponding value. Now in another program I want to call upon this program and then pretty much display the string and use the int value for a certain purpose.

public class MainMenuofgame extends JFrame implements ActionListener{
JButton slow, medium, fast;
JLabel pic1, pic2, pic3, pic4;
JTextField username;
Container frame;

static String name;
static int xspeed = 0;

public MainMenuofgame() {
super ("Main Menu of Rocket Launch");
frame = getContentPane ();
frame.setLayout (null);

pic1 = new JLabel (new ImageIcon ("welcome.png"));

pic2 = new JLabel (new ImageIcon ("name.png"));

pic3 = new JLabel (new ImageIcon ("speed.png"));

pic4 = new JLabel (new ImageIcon ("backgnd.jpg"));

username = new JTextField ();

slow = new JButton("Slow");
// slow.setActionCommand("slowspeed");
slow.addActionListener (this);

medium = new JButton("Medium");
// medium.setActionCommand("mediumspeed");
medium.addActionListener (this);

fast = new JButton("Fast");
// fast.setActionCommand("fastspeed");
fast.addActionListener (this);

pic1.setBounds (30,50, 525, 173);//welcome
pic2.setBounds (100,230,212,73);//name
pic3.setBounds (80,350,428,84);//speed

username.setBounds(310,255,150,30);

slow.setBounds (100,450,100,100);
medium.setBounds (250,450,100,100);
fast.setBounds (400,450,100,100);
//background bound goes in the end
pic4.setBounds (0,0, 600,900);

frame.add (pic1);

frame.add (pic2);

frame.add (pic3);

frame.add (username);

frame.add (slow);

frame.add (medium);

frame.add (fast);

frame.add (pic4);


setSize(600, 900);
setVisible (true);
setDefaultCloseOperation (EXIT_ON_CLOSE);

}

public void actionPerformed (ActionEvent evt){

String name = username.getText();

if (evt.getSource () == slow)
{
xspeed = 1;
}
else if(evt.getSource () == medium)
{
xspeed = 5;
}
else
{
xspeed = 10;
}

}



public static void main(String[] args) {
new MainMenuofgame ();
}

}

Answer

There are one of two ways that come to mind on how to transfer information from one program to another...

  1. Client-Server applications

This requires you to have a third application running accepting information from each of the other two application (clients) through a socket. For further information Google "Client-Server applications in Java"

  1. Have a text file passing information

To do this you should have a text file that one application stores information in and the other application just simply reads it... This is an easier solution but is less of a learning experience. Here is example code.

Application 1:

private void storeMessage(String msg){
    File centralFile = new File("path to your file");
    BufferedWriter writer = new BufferedWriter(new FileWriter(centralFile));
    writer.write(msg);
    writer.close();
}

Application 2:

private String getMessage(){
    File centralFile = new File("path to your file");
    String msg = "";
    BufferedReader reader = new BufferedReader(new FileReader(centralFile));
    while (reader.hasNextLine()){
        msg += reader.nextLine();
    }
    reader.close();
    return msg;
 }

Hope this helps