How do I add datum to System.in from my program?

Discussion in 'Java' started by metfan, Oct 25, 2003.

  1. metfan

    metfan Guest

    Hi,
    Sorry, the title may not be clear. Let me describe my question.
    I wrote a program to read from the console, say, System.in,

    BufferedReader userInput = new BufferedReader(new
    InputStreamReader(System.in));
    String inputStr;
    for (;;) {
    try {
    inputStr = userInput.readLine();
    if (inputStr != null) {
    System.out.println(inputStr);
    }
    } catch (Exception e) {
    }
    }

    if the use input some data, for example, the user input
    "Hello!" and press return, the code would print a line "Hello!",
    now what I'm going to do is to simulate that the user has
    input some extra data from console, for example, if the user
    input "Hello!", the code above would print 2 lines:
    Hello!
    World!
    And no, what I want is not a snip like:
    try {
    inputStr = userInput.readLine();
    if (inputStr != null) {
    System.out.println(inputStr);
    }
    inputStr = "World!";
    System.out.println(inputStr);
    } ...

    What I really want is to simulate the date input by user from console,
    did I make myself clear? and is that possible? sorry if my question
    looks too newbie to you.





    --
    Best regards,
    Qingjia Zhu
    Q@z@J
     
    metfan, Oct 25, 2003
    #1
    1. Advertising

  2. metfan

    Cory Guest

    metfan <qjzhupublic@___NoSpam__yahoo.ie> wrote in message news:<>...
    > Hi,
    > Sorry, the title may not be clear. Let me describe my question.
    > I wrote a program to read from the console, say, System.in,
    >
    > BufferedReader userInput = new BufferedReader(new
    > InputStreamReader(System.in));
    > String inputStr;
    > for (;;) {
    > try {
    > inputStr = userInput.readLine();
    > if (inputStr != null) {
    > System.out.println(inputStr);
    > }
    > } catch (Exception e) {
    > }
    > }
    >
    > if the use input some data, for example, the user input
    > "Hello!" and press return, the code would print a line "Hello!",
    > now what I'm going to do is to simulate that the user has
    > input some extra data from console, for example, if the user
    > input "Hello!", the code above would print 2 lines:
    > Hello!
    > World!
    > And no, what I want is not a snip like:
    > try {
    > inputStr = userInput.readLine();
    > if (inputStr != null) {
    > System.out.println(inputStr);
    > }
    > inputStr = "World!";
    > System.out.println(inputStr);
    > } ...
    >
    > What I really want is to simulate the date input by user from console,
    > did I make myself clear? and is that possible? sorry if my question
    > looks too newbie to you.




    Don't quote me on this but you could just use
    System.setIn(yourInputStream)...
    Remember, however to reset System.in if you want to be able to read
    anythign from the console.
     
    Cory, Oct 25, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jean-Marc Autexier
    Replies:
    2
    Views:
    3,787
    Jean-Marc Autexier
    Aug 30, 2003
  2. Nathan Sokalski
    Replies:
    5
    Views:
    1,084
    Gaurav Vaish \(www.Edujini-Labs.com\)
    Jan 10, 2007
  3. Nathan Sokalski
    Replies:
    4
    Views:
    341
    Nathan Sokalski
    Dec 21, 2006
  4. Nathan Sokalski
    Replies:
    4
    Views:
    378
    Nathan Sokalski
    Dec 21, 2006
  5. Duncan Harris
    Replies:
    7
    Views:
    122
    Anno Siegel
    Feb 13, 2005
Loading...

Share This Page