Connecting Tech Pros Worldwide Forums | Help | Site Map

Scanner Keyboard Accepting a blank

Newbie
 
Join Date: Nov 2009
Posts: 1
#1: 4 Weeks Ago
I am taking my first java class as i am a Database person this is quite interesting and here is my problem.

We are supposed to be learning to overload a class and i believe i have my code correct the problem is I cannot get the keyboard.nextdouble to accept me hitting enter and it having a null to make my overloaded method work.

Here is the code i have.

I am supposed to be able to just add in one, two, or three variables and the methods fire off accordingly.


Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Pay
  3. {
  4.    public static void main(String[] args)
  5. {
  6. /*Declaration of variables*/
  7.     double hoursworked;
  8.      double payrate;
  9.      double withholdingrate;
  10.      double grosspay;
  11.      double netpay;
  12.  
  13. /*This lets the user enter a numer*/     
  14.      Scanner keyBoard = new Scanner(System.in);
  15.      System.out.print("Please enter the hours you have worked.");
  16.      hoursworked = keyBoard.nextDouble();
  17.     System.out.print("Please enter your rate of pay.");
  18.      payrate = keyBoard.nextDouble();
  19.     System.out.print("Please enter your withholding rate as a decimal.");
  20.      withholdingrate = keyBoard.nextDouble();
  21.  
  22.  
  23.  
  24. /*This calls the computenetpay and displays the value*/
  25.  
  26.     computenetpay(hoursworked, payrate, withholdingrate);
  27. }
  28. /*This is a new class to calculate gross and net pay*/
  29. public static void computenetpay(double hoursworked, double payrate, double withholdingrate)
  30. {
  31.     double grosspay;
  32.    double netpay;
  33.  
  34.     grosspay = (hoursworked * payrate);
  35.     netpay = grosspay - (grosspay * withholdingrate);
  36.  
  37.    System.out.println   ("Your gross pay is " + (grosspay) + ".");    
  38.     System.out.println   ("Your net pay is " + (netpay) + ".");
  39.  
  40.  
  41. }
  42.  
  43. public static void computenetpay(double hoursworked, double payrate)
  44. {
  45.     double grosspay;
  46.    double netpay;
  47.  
  48.     grosspay = (hoursworked * payrate);
  49.     netpay = grosspay - (grosspay * 0.15);
  50.  
  51.    System.out.println   ("Your gross pay is " + (grosspay) + ".");    
  52.     System.out.println   ("Your net pay is " + (netpay) + ".");
  53.  
  54.  
  55. }
  56.  
  57. public static void computenetpay(double hoursworked)
  58. {
  59.     double grosspay;
  60.    double netpay;
  61.  
  62.     grosspay = (hoursworked * 5.85);
  63.     netpay = grosspay - (grosspay * 0.15);
  64.  
  65.    System.out.println   ("Your gross pay is " + (grosspay) + ".");    
  66.     System.out.println   ("Your net pay is " + (netpay) + ".");
  67.  
  68. }
  69.  
  70. }

Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,963
#2: 4 Weeks Ago

re: Scanner Keyboard Accepting a blank


I imagine that the problem is this line:
Expand|Select|Wrap|Line Numbers
  1. computenetpay(hoursworked, payrate, withholdingrate);
You will *always* be calling the method that accepts 3 doubles, even if any of them do not have a value to work with.

You need to check that the values are present and call the relevant method.

Expand|Select|Wrap|Line Numbers
  1. double x, y, z;
  2.  
  3. /** get input */
  4. if x, y, z not null then
  5.     func(x, y, z)
  6. else if x, y not null then
  7.     func(x, y)
  8. else if x not null then
  9.     func(x)
  10. else
  11.     /** no values give */
  12.     explode()
  13. endif
  14.  
Mark.

P.S. Please wrap code with [code] code goes here [/code] - as per the forum guidelines. Thanks :)
Reply