Question

Is there a way in which, from a function, I print a String in with System.out.print() and then read it from another function?

something like:

void printC(String foo){
    System.out.print(foo);
}

void read(){
    String c;
    while(something){
        printC(somethingElse);
        c = System.console.readLine();
        JOptionPane.showMessageDialog(c);
    }
}
Was it helpful?

Solution 2

Try PipedOutputStream.

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Test extends JFrame {

    void printC(String foo){
        System.out.print(foo);
    }

    void read() throws IOException{
        String c = "";
        PipedOutputStream pipeOut = new PipedOutputStream();
        PipedInputStream pipeIn = new PipedInputStream(pipeOut);
        System.setOut(new PrintStream(pipeOut));
        Scanner sc = new Scanner(pipeIn);
        while(!c.equalsIgnoreCase("Quit")){
        printC("Test\n");
        c = sc.nextLine();
        JOptionPane.showMessageDialog(this, c);
        }
    }

    public static void main(String[] args) throws IOException {
        Test t = new Test();
        t.read();
    }

}

OTHER TIPS

No, you can't. As other people have commented, you probably just want an internal data structure to connect different components.

In command-line programs, the standard input and standard output (plus standard error) are completely independent streams. It's typical for all three to be connected to a single virtual terminal, but they can be redirected independently from the shell, such as by using a pipeline or files.

Think about what if the input of your program is coming from a file and the output is being piped to another program; trying to "get back" the output doesn't make any sense.

Why do you want to do this? Is it so that you can print something to the screen or so that you can create events?

If you particularly want to pass messages to the screen AND also another part of your application; a simple solution could involve creating your own PrintStream class. You can deal with the object in the same way as you would otherwise deal with System.out (as that's a PrintStream too).

Something along the lines of this:

public class FancyStream extends PrintStream
{
    LinkedList<String> messageQueue = new LinkedList<>();

    @Override
    public void println(String line)
    {
        System.out.println(line);
        messageQueue.add(line);
    }

    public String getLine()
    {
        return messageQueue.pop();
    }
}

However, if you want events (as you've suggested in the comments), this is not the way to do it! You should take a look at the Observer pattern for dealing with events. The wikipedia article about this is here.

There's plenty of other resources to learn about the Observer pattern if you do a Google search. Java even has a built in Observable class and Observer interface that may solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top