Jump to content
  • Advertisement
Sign in to follow this  
ProperFuked

[java] newbie java question

This topic is 4337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

well, maybe not so newbie, but anyway.. i have 2 separate classes, class A and class B, both have main methods. when run, class A produces console output. i want to run class A from class B and capture the console output produced by class A into B so that i can use it in B. The output produced by class a is a String, so capturing it into a String in B would suffice. I was wondering if anyone has any ideas on how i would go about doing this?? Please and thank you.

Share this post


Link to post
Share on other sites
Advertisement
You need the output from A to be generated in some function that returns the output. B can then call that function.

class ClassA
{
public static void main(String [] args)
{
System.out.writeln(ClassA.getOutput());
}

public static void getOutput()
{
return "This is my output.";
}
}

class ClassB
{
protected static String capturedOutput;

public static void main(String [] args)
{
ClassA aInstance = new ClassA();
capturedOutput = aInstance.getOutput();
}
}



Share this post


Link to post
Share on other sites
Hi there,
I think that what you might have here is a design problem. Why do both of your classes have a "main" method? Usually programs have only 1 entry point. If you wanted to programmatically use a String object produced by class A in class B, then you could have something like:



public class A{
public String getString(){
return new String("Foobar");
}
}

public class B{
public static void main(String[] args){
A a = new A();
String str = a.getString();
// Do something with the String
}
}




In 2 different files called A.java and B.java respectively. Before compiling both and running the main method in class B.

There is no reason for class A to send its String output to the console if the aim is for class B to capture that output as a String.

Jon

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!