Erkennung leerer Eingabe (JAVA)

Also mein Programm hat zu zählen, die Höhe der Veränderung einer person, die in der scanner-Methode, die jedoch zwei Fehler muss pop-out, wenn a) die Eingabe ist keine Zahl und b) wenn der input fehlt. Ich habe eine Menge ärger mit der letzteren.

import java.util.InputMismatchException;
import java.util.Scanner;
public class MakingChange 
{
public static void main(String[] args)
{

    Scanner input = new Scanner(System.in);  //Reading from System.in
    System.out.print("How much money do you have: ");

    double amountInDollars = 0;

        try {
                amountInDollars = input.nextDouble();
        } catch (InputMismatchException e) {
                System.out.println("INVALID"); //print invalid
        return;
        }

        if (input.equals(" ")){
            System.out.println("INVALID");
            return;
        }


        int amountInPennies = (int) Math.round(amountInDollars * 100);
        amountInPennies = (int) (Math.round(amountInPennies / 5.0)  * 5);

        //toonies
        int numberofToonies = (int)(amountInPennies / 200.00);
        amountInPennies = amountInPennies % 200;
        //loonies   
        int numberofLoonies = (int) (amountInPennies / 100.00);
        amountInPennies = amountInPennies % 100;
        //quarters
        int numberofQuarters = (int)(amountInPennies / 25.00);
        amountInPennies = amountInPennies % 25;
        //dimes      
        int numberofDimes = (int)(amountInPennies / 10.00);
        amountInPennies = amountInPennies % 10;
        //nickels  
        int numberofNickels = (int)(amountInPennies / 5.00);

System.out.println("toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:" + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels); 

}   
}

Also die erwartet werden sollte "UNGÜLTIG" aber derzeit sind alle zurückgibt, ist ein leerer Platz.
Danke!

InformationsquelleAutor Jane Doe2 | 2016-06-13
Schreibe einen Kommentar