-1

My class does an ouput (with System.out.println()) and the other class should take this output as an input (with Scanner scan.hasNextInt(), and scan.nextInt()). Do you know, how my Scanner recognize this output as an input? (As I cannot press enter afterwards or something.)

@Override
public void actionPerformed(ActionEvent e) { 
    System.out.println(e.getActionCommand()); 
} 

So this does the output (its Buttons... the Action Command are the coordinates x + y. This should takes the output as an input:

if(scan.hasNextInt()) { 
    x = scan.nextInt();
    if(x < 0 || x >= this.length) { 
        System.out.println("Falsche Eingabe! Nochmal!"); 
        count--; return; 
    } 
}
DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
  • 2
    Please share your code. – 4castle Oct 31 '16 at 15:56
  • 1
    it might be better to call a method existing in the other class, and pass the String you're printing out into it as a parameter. – Chains Oct 31 '16 at 15:58
  • @Override public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); } So this does the output (its Buttons... the Action Command are the coordinates x + y. This should takes the output as an input: if(scan.hasNextInt()) { x = scan.nextInt(); if(x < 0 || x >= this.length) { System.out.println("Falsche Eingabe! Nochmal!"); count--; return; } } – Markus Hoffmann Oct 31 '16 at 16:00
  • Using an Observer would be clean. Or you could share a stream to link those two. Off course this depends on the type of relation you want. In general, the Observer is enough because it will trigger the update methods on call, no need to block a thread on a listening task. – AxelH Nov 03 '16 at 06:50
  • Using your code, you will need to open the Scanner on the same Stream (System.out) to listen on it (not sure how this will work with this stream one to be fair). With this, both instance won't need to know each other. – AxelH Nov 03 '16 at 06:52

1 Answers1

0

If the two classes are part of the same process (same JVM), then you should have one class pass a String to a method of the other class (but, if the second class will be parsing that String, you should consider passing actual values [ints], or a POJO - a Java object with getter methods as method parameter[s]).

If the two classes are in separate processes [you have two main(...) methods], then this is could be an operating system question, or you might consider using Java-based Inter-Process Communication (sockets, shared memory, shared file, etc.)

For example, in Unix (and unix-like) Operating Systems, you can pipe (redirect) the output of one process to be the input of another process.

Here's to a question about Java Inter-Process Communication:

How to have 2 JVMs talk to one another

Community
  • 1
  • 1
ronw
  • 1
  • 2