Manderson
Manderson

Reputation: 13

get strings from other classes java

Just need someone to quickly show me how I can get the macAddress in the Address class to generate in the Network device class, thanks.

Address class

public class Address {

public static void main(String args[]){
Random rand = new Random();

    String result1 = String.format("%x",(int)(Math.random()*100));
    String result2 = String.format("%x",(int)(Math.random()*100));
    String result3 = String.format("%x",(int)(Math.random()*100));
    String result4 = String.format("%x",(int)(Math.random()*100));
    String result5 = String.format("%x",(int)(Math.random()*100));
    String result6 = String.format("%x",(int)(Math.random()*100));

String macAddress = (result1 + ":" + result2 + ":" + result3 + ":" + result4 + ":" + result5 + ":" + result6);
}
}

NetworkDevice class

public class NetworkDevice {
//get it to generate a new address from address class and print it out


public static void main (String args[]){
    Address thisAddress = new Address();
    System.out.println(thisAddress);

}
}

Upvotes: 0

Views: 44

Answers (1)

WIR3D
WIR3D

Reputation: 532

So change main in the Address class into a function like so:

public class Address {

    public static String Macgen() {
        Random rand = new Random();

        String result1 = String.format("%x", (int) (Math.random() * 100));
        String result2 = String.format("%x", (int) (Math.random() * 100));
        String result3 = String.format("%x", (int) (Math.random() * 100));
        String result4 = String.format("%x", (int) (Math.random() * 100));
        String result5 = String.format("%x", (int) (Math.random() * 100));
        String result6 = String.format("%x", (int) (Math.random() * 100));

        String macAddress = (result1 + ":" + result2 + ":" + result3 + ":" +

        result4 + ":" + result5 + ":" + result6);
        return macAdress;
    }
}

Call this function like so

public class NetworkDevice {

public static void main (String args[]){
    System.out.println(Address.Macgen());

}

Upvotes: 2

Related Questions