Article View: aus.computers.java
Article #1184Re: How to input Decimal figures in a input line
From: Tom Davies
Date: Sun, 04 Jul 1999 00:00
Date: Sun, 04 Jul 1999 00:00
79 lines
3362 bytes
3362 bytes
You are parsing your string as an integer. Try parsing with
Double.valueOf(), and note that you'll have to catch a
NumberFormatException and print an error message.
If you are unsure of the difference between an integer and a real number
ask and I'll explain it.
Hope this helps.
Tom
mrbafner wrote:
>
> Hi,
>
> How can you input numbers in decimal form, here is the program I am working
> on, but I cannot input decimal number (example 4.50). If this is possible I
> would really appreciate the response, thanks.
>
> ===========================================================
>
> import java.io.*;
> public class PayPacket {
>
> final static double Tax_Rate = 0.3; // tax rate percentage
>
> public static void main (String[] args)
> throws IOException
> {
> System.out.println ("SALES TAX CALCULATOR"); // create a text input
> stream
>
> BufferedReader stdin = new BufferedReader
> (new InputStreamReader (System.in));
>
> // this allow people to input something, then the computer can work of
> these figures
>
> String initString;
> double HourlyRate; // stores user input
> double NoOfHours; // stores user input
> double GrossPay; // stores salary before tax tax
> double tax; // stores payable tax
> double NetPay; // stores pay after tax
>
> System.out.print ("Enter the Hourly Rate: " + "\n");
> initString = stdin.readLine();
> HourlyRate = Integer.parseInt (initString); // calculate payable
> tax
>
> System.out.print ("Number of Hours Worked: \n");
> initString = stdin.readLine();
> NoOfHours = Integer.parseInt (initString);
>
> GrossPay = HourlyRate * NoOfHours ;
>
> if (GrossPay >0 )
> {
> tax = GrossPay * Tax_Rate ;
> }
>
> else
> {
> tax = 0;
> } // calculate NetPay
>
> NetPay = GrossPay - tax; // Output NetPay
>
> System.out.println();
> System.out.println("Hourly Rate : " + HourlyRate );
> System.out.println("Hours Worked : " + NoOfHours );
> System.out.println("Gross Pay : " + GrossPay );
> System.out.println("Total Tax (" + Tax_Rate + " %): " + tax );
> System.out.println("--------------------------------");
> System.out.println("Net Pay : " + NetPay );
> }
> }
>
> =================================================================
Technical Details
Message-ID:
<377E22D6.D8650B5A@smartchat.net.au>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<iP8f3.257$ZS2.16444@nsw.nnrp.telstra.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,e8b377bae8907342 X-Google-Attributes: gid10d6db,public From: Tom Davies <tom@smartchat.net.au> Subject: Re: How to input Decimal figures in a input line Date: 1999/07/04 Message-ID: <377E22D6.D8650B5A@smartchat.net.au> X-Deja-AN: 496653436 Content-Transfer-Encoding: 7bit References: <iP8f3.257$ZS2.16444@nsw.nnrp.telstra.net> To: mrbafner <mrbafner@ozlinx.com.au> Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@connect.com.au X-Trace: merki.connect.com.au 930973190 23691 210.8.3.130 (3 Jul 1999 03:39:50 GMT) Organization: Customer of Connect.com.au Pty. Ltd. Mime-Version: 1.0 NNTP-Posting-Date: 3 Jul 1999 03:39:50 GMT Newsgroups: aus.computers.java