Article View: borland.public.jbuilder.java.language
Article #18786Re: Getting Input
From: Clint Brewer
Date: Tue, 28 Apr 1998 00:00
Date: Tue, 28 Apr 1998 00:00
55 lines
1975 bytes
1975 bytes
Emmanuel Vlamakis wrote:
>
> Hello, I'm new to Java and trying to learn it on my own. One thing I
> haven't yet been able to figure out is how do I get user input from the
> keyboard. For example, in C++ I might have...
>
> int main()
> {
> int Temp;
>
> cout <<"Please enter a number: ";
> cin >>Temp;
>
> return 0;
> }
>
> How would I write the equivalent code in Java? Please show me an example.
>
> Best Regards,
>
> Emmanuel Vlamakis
Hello Emmanuel,
In Java the input stream is System.in
and the output stream is System.out
so, to print a line:
System.out.println("please enter a number:");
and to read a line,
byte AByteArray[10];
System.in.read(AByteArray);
this isn't very friendly to integers or any other type other than byte,
so java.io provides DataInputStream to take care of this problem.
Now , DataInputStream takes an inputstream as a parameter to it's
constructor, so you can just pass System.in.
and this our code is
/////////////////////////////////////////////////////
DataInputStream s = new DataInputStream(System.in);
System.out.println("please enter a number:");
int theNumber;
theNumber=s.readInt();
/////////////////////////////////////////////////////
Take a look at the DataInputStream class, since it should have
just about all the functionality you need.
Hope this helps!
Clint Brewer
Technical Details
Message-ID:
<35467F6C.283C@borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<6i10vt$6ic9@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,e7144664b404f5 X-Google-Attributes: gidf74b3,public From: Clint Brewer <supportinfo@borland.com> Subject: Re: Getting Input Date: 1998/04/28 Message-ID: <35467F6C.283C@borland.com> X-Deja-AN: 348601320 Content-Transfer-Encoding: 7bit References: <6i10vt$6ic9@forums.borland.com> To: Emmanuel Vlamakis <E-Vlamakis@govst.edu> Content-Type: text/plain; charset=us-ascii Organization: Borland International Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language