• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accepting Numeric User Input

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a Java program that calculates payroll and overtime pay. The user enters in the number of hours worked and the pay rate. My problem is that I cannot get the program to except numerical input from the keyboard. It's using unicode or something because the calculations are all wrong when I desk check the program. How do you get Java to read 40 hours as an integer of 40 and a double pay rate of $10.00 as double variable of 10.00 from the keyboard as it is entered in by the user. I have been using the System.in.read() method for user input.
Here's what I have so far:
public class Pay
{
public static void main(String[] args) throws Exception
{
double payRate;
int hoursWorked;
double regularPay;
double overTimePay;

System.out.println("Enter the hours worked:");
hoursWorked=(int)System.in.read();
System.in.read();
System.out.println("Enter in the pay rate:");
payRate=(double)System.in.read();
System.in.read();

if(hoursWorked == 50)
{
regularPay = payRate * 40;
overTimePay = (hoursWorked - 40) * (payRate * 2);
}
else if(hoursWorked == 45)
{
regularPay = payRate * 40;
overTimePay = (hoursWorked - 40) * (payRate * 2);
}
else if(hoursWorked == 40)
{
regularPay = payRate * 40;
overTimePay = 0.0;
}
else System.out.println("Invalid number of hours");

System.out.println("The number of hours worked is " + hoursWorked);
System.out.println("The hourly rate is " + payRate);
System.out.println("The regular pay is " + regularPay);
System.out.println("Overtime pay is " + overTimePay);
}

}
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uhh, the more knowledgeable people may want to correct me, but I think that one reads text using something else, like:
, not using system.in the way one uses system.out. In shorter terms, one has to make an instance of the JAVA reader in order to read input. Also, as a side note, you may have to include try/catch for IOExceptions.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might try this (or something like it) to get your data,.
System.out.print("Enter the hours worked:");
BufferedReader B = new BufferedReader(new InputStreamReader(System.in));
String S = B.readLine();
float N = Float.parseFloat(S);
// int would be input by
// int N = Integer.parseInt(S);

JL
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic