0

I've been searching on the internet and can't seem to find this answer So does anyone know to how stop users from inputting letters where only numbers should be allowed?

This is what my code looks like so far.

public static double payCalculator(double hours, double basePay)
{
    double totalPay; 
    double overTime = 8.00 * 1.5;
    while(hours < 0 | hours > 60) {
        System.out.println("Cannot work more than 60 hours a week");
        System.out.println("Hours Work?");
        hours = in.nextDouble();
    }
    while(basePay < 8) {
        System.out.println("Base Pay cannot be less than 8");
        System.out.println("Base Pay?");
        basePay = in.nextDouble();
    }
    if 
        (hours <= 40){
        totalPay = hours*basePay;
    }
    else {
        totalPay = ((hours - 40)*overTime) + (40*basePay);
    }
    return totalPay;

}
public static void main (String[] args) {

    System.out.println("Hours worked?");
    hours = in.nextDouble();
    System.out.println("Base Pay?");
    basePay = in.nextDouble();
    DecimalFormat df = new DecimalFormat("###.##");
    totalPay = payCalculator(hours, basePay);
    System.out.println("Total Pay is " + df.format(totalPay));       
}
}

Thank you for your time.

4
  • You don't need to post your entire code to find the solution to your problem, please share only that part of your code which is related to your problem. Commented Jan 30, 2015 at 13:17
  • Seems to be a console application. Is a GUI with Swing input text field possible. Commented Jan 30, 2015 at 13:23
  • Sufiyan Ghori - all the code is related to my problem, I'm not sure where the answer would go. Commented Jan 30, 2015 at 13:35
  • @KorwynnWagstaff, not all the code is related, for instance, code in your main method is enough to understand what you are asking for, therefore there was no need to include payCalculator() method with your code. Commented Jan 30, 2015 at 14:18

1 Answer 1

5

I assume your are using Scanner to take input.

You can use Scanner.hasNextDouble() to verify it is the number, it returns true if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. The scanner does not advance past any input.

Consider this example, It will keep asking for the input unless the user provides it with a number,

    Scanner sc = new Scanner(System.in);
    double dbl = 0.0;
    boolean isValid = false;
    while (isValid == false) {
        System.out.println("Input Number: ");
        // If input is number execute this,
        if (sc.hasNextDouble()) {
            dbl = sc.nextDouble();
            isValid = true;
            System.out.println("OK");
        }
        // If input is not a number execute this block, 
        else {
            System.out.println("Error! Invalid number. Try again.");
        }
        sc.nextLine(); // discard any other data
    }
    sc.close();

output,

Input Number: 
adsads
Error! Invalid number. Try again.
Input Number: 
sdas
Error! Invalid number. Try again.
Input Number: 
hello
Error! Invalid number. Try again.
Input Number: 
hi
Error! Invalid number. Try again.
Input Number: 
2.0
OK
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.