Article View: borland.public.jbuilder.java.language
Article #19318Re: read doubles from console
From: Andrew Bennett
Date: Thu, 19 Feb 1998 00:00
Date: Thu, 19 Feb 1998 00:00
52 lines
1950 bytes
1950 bytes
angus fraser wrote:
>
> Hi,
>
> A beginners type question.
>
> I want to read 2 double(temperature) values entered via the console(it's a
> fairly trivial console program) but I can't get it to work using the
> following :-
>
> double dry ;
> double wet ;
> try {
> DataInputStream dis = new DataInputStream( System.in );
> System.out.println("Enter Temp 1 > ");
> dry = dis.readDouble();
> System.out.println("Enter Temp 2 > ");
> wet = dis.readDouble();
> }
> catch ( IOException e ) { }
>
> What is the correct way to read float/double values entered via a console
> window
Because people can type in strings there, say "aaaa", you need
to test to ensure what they entered was a legal value.
So, pull a string. Then for your Float, use java.lang.Float's
valueOf() method. If it doesn't parse, it'll throw an exception,
so you can trap that.
java.lang.Double() has a similiar valueOf() method. To parse a
string to an integer, use parseInt().
Hope that helps.
Andrew
--
Andrew Bennett
abennett@borland.com
Due to my workload, personal email more than likely won't get a reply.
Support options are detailed at http://www.borland.com/devsupport/
I am unable to answer support questions via email at this time.
Check http://www.borland.com/devsupport/jbuilder/ for FAQs and
additional JBuilder support related help.
Technical Details
Message-ID:
<34EC8353.1A7BF514@borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<memo.19980215231440.217A@afraser.cix.co.uk>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,c6c624e45e8aa49f X-Google-Attributes: gidf74b3,public From: Andrew Bennett <abennett@borland.com> Subject: Re: read doubles from console Date: 1998/02/19 Message-ID: <34EC8353.1A7BF514@borland.com> X-Deja-AN: 326681579 Content-Transfer-Encoding: 7bit References: <memo.19980215231440.217A@afraser.cix.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: None Newsgroups: borland.public.jbuilder.java.language