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

M

metfan

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.
 
C

Cory

metfan said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top