[3868] in java-interest
Re: how to read from stdin?
daemon@ATHENA.MIT.EDU (Per Danvind)
Thu Nov 30 05:22:30 1995
To: michael@w3media.com (Michael Mehrle)
cc: grege@optimation.com.au (Greg Ewing), java-interest@java.sun.com
In-reply-to: Your message of "Wed, 29 Nov 1995 14:45:13 MST."
<199511292250.OAA15284@vv.val.net>
Date: Thu, 30 Nov 1995 09:49:12 +0100
From: Per Danvind <perry@cdt.luth.se>
> >Hi,
> >
> >Could someone please guide me as to how I can read input
> >from stdin?
> >
> >I've tried stuff like:
> >
> >DataInputStream dataStream = new DataInputStream(System.in);
> >String input = dataStream.readLine();
> >
> >Perhaps I should open a FileInputStream instead?
> >
> >Thanks for your time
> >
> >GREG
> >
> >grege@optimation.com.au
> >
> >
> >
>
> I'll throw in my 2 cents!
>
> import java.io.DataInputStream;
>
> class ReadInput {
>
> public static void main(String args[]) {
>
> String line;
> DataInputStream myDataInputStream;
>
> myDataInputStream = new DataInputStream(System.in);
>
> do {
> line = myDataInputStream.readLine();
> .... // do what you must with it ;)
>
> }
>
> }
>
> That *should* work, lemme know!
>
> I however would like to know how to accept several lines in the body of the
> "do" loop and write them into an string array! Anybody?
>
>
>
> Michael
Yep, it should work, it does for me at least. Here is a piece of
code that reads lines from stdin until the user types 'exit'.
import java.io.*;
import java.util.*;
public class ReadLines {
public static void main(String args[]) {
DataInputStream d = new DataInputStream(System.in);
Vector lines = new Vector();
String line = "";
boolean exit = false;
while(!exit) {
try
line = d.readLine();
catch(IOException e)
line = "exit";
// Does the user want to exit?
if(line.equalsIgnoreCase("exit"))
exit = true;
else
lines.addElement(line);
}
// Show the lines that were read.
System.out.println("The read lines...\n");
Enumeration enum = lines.elements();
while(enum.hasMoreElements())
System.out.println("-> "+(String) enum.nextElement());
}
}
--
Per
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com