Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8006845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:38:40+00:00 2026-06-04T17:38:40+00:00

I wanted to know if there was a way to take a String –

  • 0

I wanted to know if there was a way to take a String – let’s say:

String str = "blabla";

and do:

System.in.setText(str);   

I know this does not work – I wanted to know if there was a way to do this. And then send that same string. Just like if you would write in the console and press Enter.

It is a program with a server socket and I am trying to send a String over a port, so that the other application would know what to do with it.

EDIT :
I found a way to redirect the inputstream to a Textfield when the user writes in the Textfield it sends it over the System.in.

import java.io.*;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class TextfieldInputStream extends InputStream implements DocumentListener {

    private JTextField tf;
    private String str = null;
    private int pos = 0;

    public TextfieldInputStream(JTextField jtf) {
        tf = jtf;
    }

    @Override
    public int read() {
        //test if the available input has reached its end
        //and the EOS should be returned 
        if(str != null && pos == str.length()){
            str = null;
            //this is supposed to return -1 on "end of stream"
            //but I'm having a hard time locating the constant
            return java.io.StreamTokenizer.TT_EOF;
        }
        //no input available, block until more is available because that's
        //the behavior specified in the Javadocs
        while (str == null || pos >= str.length()) {
            try {
                //according to the docs read() should block until new input is available
                synchronized (this) {
                    this.wait();
                }
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        //read an additional character, return it and increment the index
        return str.charAt(pos++);
    }

    public void changedUpdate(DocumentEvent e) {
        // TODO Auto-generated method stub
    }

    public void insertUpdate(DocumentEvent e){
        str = tf.getText() + "\n";
        pos = 0;
        synchronized (this) {
            //maybe this should only notify() as multiple threads may
            //be waiting for input and they would now race for input
            this.notifyAll();
        }
    }

    public void removeUpdate(DocumentEvent e){
        // TODO Auto-generated method stub
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:38:43+00:00Added an answer on June 4, 2026 at 5:38 pm
    package test.t100.t007;
    
    import java.io.ByteArrayInputStream;
    import java.util.Scanner;
    
    public class SystemIn {
    
        public static void main(String[] args) {
            String str = "blabla";
            ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
            System.setIn(bais);
            // We might use the ByteArrayInputStream here, but going with System.in..
            Scanner scanner = new Scanner(System.in);
            String input = scanner.next();
            System.out.println(input);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to know if there is way to log the mysql queries in
I wanted to know if there's a way I can bind an Asp.net Ajax
I wanted to know if there is any way to synchronize (or exchanging files)
I just wanted to know if there is a way on how to remove
hi guys I wanted to know, is there a way to not display my
I wanted to know is there any way to retrieve all the handles associated
I want to know is there any way to split text like this: 123456789
Just wanted to know is there a way to protect an application from getting
I wanted to know if there's any way I could output HTML after extracting
I wanted to know if there is a way to get in a One2Many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.