🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: aus.computers.java
2 messages
2 total messages Started by mrbafner Sat, 03 Jul 1999 00:00
How to input Decimal figures in a input line
#1183
Author: mrbafner
Date: Sat, 03 Jul 1999 00:00
71 lines
2843 bytes
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 );
   }
 }

=================================================================
Re: How to input Decimal figures in a input line
#1184
Author: Tom Davies
Date: Sun, 04 Jul 1999 00:00
79 lines
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 );
>    }
>  }
> 
> =================================================================
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads