Scanner Keyboard Accepting a blank

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aycex
    New Member
    • Nov 2009
    • 1

    Scanner Keyboard Accepting a blank

    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.nextdo uble 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.


    Code:
    import java.util.Scanner;
    public class Pay
    {
       public static void main(String[] args)
    {
    /*Declaration of variables*/
        double hoursworked;
    	 double payrate;
    	 double withholdingrate;
    	 double grosspay;
    	 double netpay;
    	 
    /*This lets the user enter a numer*/	 
    	 Scanner keyBoard = new Scanner(System.in);
    	 System.out.print("Please enter the hours you have worked.");
    	 hoursworked = keyBoard.nextDouble();
        System.out.print("Please enter your rate of pay.");
    	 payrate = keyBoard.nextDouble();
        System.out.print("Please enter your withholding rate as a decimal.");
    	 withholdingrate = keyBoard.nextDouble();
    
    
    
    /*This calls the computenetpay and displays the value*/
    
    	computenetpay(hoursworked, payrate, withholdingrate);
    }
    /*This is a new class to calculate gross and net pay*/
    public static void computenetpay(double hoursworked, double payrate, double withholdingrate)
    {
    	double grosspay;
       double netpay;
    
    	grosspay = (hoursworked * payrate);
    	netpay = grosspay - (grosspay * withholdingrate);
    
       System.out.println   ("Your gross pay is " + (grosspay) + ".");	
    	System.out.println   ("Your net pay is " + (netpay) + ".");
    
    
    }
    
    public static void computenetpay(double hoursworked, double payrate)
    {
    	double grosspay;
       double netpay;
    
    	grosspay = (hoursworked * payrate);
    	netpay = grosspay - (grosspay * 0.15);
    
       System.out.println   ("Your gross pay is " + (grosspay) + ".");	
    	System.out.println   ("Your net pay is " + (netpay) + ".");
    
    
    }
    
    public static void computenetpay(double hoursworked)
    {
    	double grosspay;
       double netpay;
    
    	grosspay = (hoursworked * 5.85);
    	netpay = grosspay - (grosspay * 0.15);
    
       System.out.println   ("Your gross pay is " + (grosspay) + ".");	
    	System.out.println   ("Your net pay is " + (netpay) + ".");
    
    }
    	
    }
    Last edited by Frinavale; Nov 4 '09, 10:11 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    I imagine that the problem is this line:
    Code:
    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.

    Code:
    double x, y, z;
    
    /** get input */
    if x, y, z not null then
        func(x, y, z)
    else if x, y not null then
        func(x, y)
    else if x not null then
        func(x)
    else
        /** no values give */
        explode()
    endif
    Mark.

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

    Comment

    Working...