1

I'm working on a rather larger project and this is just included in it. Basically I created a class that takes an employee's info, we need to have it so the user must enter in a double for their pay rate. Say 8.00. Note the user CANNOT add in a dollar sign such as $8.00, else it will prompt the user an error has occured. Yet when I test run the code, instead of using the error prompt, the code simply crashes.

public static double getPayRate()
 {
    double payRate;
    System.out.println("Please enter the employee's pay rate (a numeric value): ");
    payRate = console.nextDouble();
    while(console.hasNextDouble())
    {
        System.out.println("An error occured with your input...");
        System.out.println("Please enter the employee's pay rate (a numeric value): ");
        payRate = console.nextDouble();
    }
    return payRate;
 }
}
Leoinu
  • 91
  • 2
  • 4
  • 17
  • Can we get a stacktrace? At first glance, console could be undefined (which would throw a NPE). – Joseph Boyle Mar 13 '14 at 22:19
  • 6
    Do I need to sound the usual warning about using floating-point types to store an amount of money? – Dawood ibn Kareem Mar 13 '14 at 22:20
  • From the [Javadoc](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextDouble()) _This method will throw InputMismatchException if the next token cannot be translated into a valid double value_. You need to catch the exception. Or better yet read the value as a `String` then parse it using `Double.parseDouble` after checking. – Boris the Spider Mar 13 '14 at 22:25
  • Well the employee name, pay rate hours and his employee number must be validated. So I don't think I can parse it. – Leoinu Mar 13 '14 at 22:33
  • possible duplicate of [Error handling reading ints from Scanner in while loop](http://stackoverflow.com/questions/22393156/error-handling-reading-ints-from-scanner-in-while-loop) – Jason C Mar 14 '14 at 00:33

1 Answers1

0

Okay so using the code you have, a way to go about this is like so.. While this code works to retrieve a double value, it is not recommended to use double when dealing with money, refer to this post for more information. Why not use Double or Float to represent currency?

import java.util.Scanner;

public class SOTest {

    public static Scanner console = new Scanner(System.in);

    public static double getPayRate()
     {
        while(true)
        {
            System.out.println("Please enter the employee's pay rate (a numeric value):");
            try {
                return Double.parseDouble(console.nextLine());
            } catch(NumberFormatException ex) {
                System.out.println("An error occured with your input...");
            }
        }
     }

    public static void main(String[] args) {
        System.out.println(getPayRate());
    }
}
Community
  • 1
  • 1
Tristan
  • 2,349
  • 16
  • 11
  • Downvote removed as the code is no longer brazenly wrong. It's still horribly messy and I would not recommend that anyone actually use it however. You could just `while(true)` and return the result of the parse directly. – Boris the Spider Mar 13 '14 at 22:36
  • I never said I recommended it. I showed him how to do code that would work for what he is trying to do. – Tristan Mar 13 '14 at 22:37
  • 2
    By answering you _have_ recommended it. – Boris the Spider Mar 13 '14 at 22:38
  • Posting an answer containing code constitutes recommending that code. If you don't recommend it, don't post it. – Dawood ibn Kareem Mar 13 '14 at 22:38
  • I recommend it as a fix for his current code. It works as intended. You are just picking a fight for no reason. Like I said, go answer the question yourself then. – Tristan Mar 13 '14 at 22:39
  • No need to argue you two. Thank you Tristan for the help. It is much appreciated. – Leoinu Mar 14 '14 at 00:08