Alexandru Cimpanu
Alexandru Cimpanu

Reputation: 1069

java scanner input

Can I use the scanner from java to get the output from one class and use it as input for another class? I have a JFrame that prints a message like so:

if(jButton1.getText().compareTo("SEND")>=0){
        String msg;
        int cod=0;
        if(jRadioButton1.isSelected()){ 
            if(jTextField1.getText().length()>=2) cod = Integer.parseInt(jTextField1.getText().substring(0, 2));
            if (codes.get(cod)!=null){
                msg=codes.get(cod).concat(jTextField1.getText().substring(2));
                System.out.println(msg);
            }
            jTextField1.setText("");
        }
        else{
            msg=jTextField1.getText();
            jTextField1.setText("");
            System.out.println(msg);
        }
    }

and a class that uses a scanner to get messages from console:

Scanner scanIn = new Scanner(System.in);
for (int i = 0; i < NUM_MSGS; i++) {
    s = scanIn.nextLine();
    message.setText( "This is message " +s );
    System.out.println("Sending message: " + message.getText());
    producer.send(message);
}

Upvotes: 0

Views: 1515

Answers (1)

ddmps
ddmps

Reputation: 4380

I think you can do that in some complicated way but may I suggest just calling a method instead. Let's say you want input from both the console and the other class. Then you can make a method doSomethingWithInput(String msg) that is called from both the scanner class and the JFrame.

Upvotes: 0

Related Questions