3

So my program has to count the amount of change a person enters in the scanner method, however, two errors must pop out if a) the input is not a number and b) if the input is lacking. I am having a lot of trouble with the latter.

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); 

}   
}

So the expected should be "INVALID" but currently, all that returns is a blank space. Thanks!

Jane Doe2
  • 141
  • 1
  • 2
  • 15
  • 1
    Possible duplicate of [Blank input from scanner - java](http://stackoverflow.com/questions/5689325/blank-input-from-scanner-java) – Cache Staheli Jun 13 '16 at 22:59

4 Answers4

4

Check below code, I have added comments.

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;
        String amountInString = input.nextLine();
        boolean isValidNum = false;

        if (amountInString.equals("") || amountInString.equals(" ")) { // Empty string check
            System.out.println("Empty String");
        } else if (amountInString.matches("-?\\d+(\\.\\d+)?")) { // valid double check
            amountInDollars = Double.parseDouble(amountInString);
            isValidNum = true;
        } else {
            System.out.println("Number Format error");
        }

        if (isValidNum) {
            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);
        }

    }
}
CrazyJavaLearner
  • 368
  • 8
  • 17
0

try this and should work fine for you.

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

    double amountInDollars = 0;
    String emp = input.nextLine();

    try {

        // if the user doesn't enter anything and press enter or entered value is empty
        if (emp.isEmpty() || emp.equals(" ")) { 
            System.out.println("Invalid input");
            return;
        }

    }catch (InputMismatchException e){
       //catch the message
        e.printStackTrace();
    }
    // if string entered is not double
    boolean isnum = emp.chars().allMatch(Character::isAlphabetic);

    if (!isnum) { // if is a Number then we pass it in
        amountInDollars = Double.parseDouble(emp);
    } else {
       // we pass exception message to catch
        System.out.println("Invalid Input: Double values only please");
        return;
    }

    if (amountInDollars < 0) { // if the value is 'Negative'
        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);

Let me know if it works out.

Seek Addo
  • 1,871
  • 2
  • 18
  • 30
  • This is wrong. The try/catch block will never catch an InputMismatchException, because the `input.nextLine()` statement is before the try block. Your try block is useless. Nothing inside of it will trigger an exception. Too little, too late---and it doesn't work anyway. The `.isEmpty()` method does not detect an empty input for me. – Ryan Apr 09 '23 at 03:28
-1

As far as I see, you are testing whether your scanner object equals a string literal, which makes no sense at all. First, get a string, using any of the scanner's nextXXX-methods. Then, check if that string is empty, by using someString.equals(""), or even better in my opinion, someString.isEmpty().

Cache Staheli
  • 3,510
  • 7
  • 32
  • 51
Silverclaw
  • 1,316
  • 2
  • 15
  • 28
-1
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    BigDecimal money = getBigDecimal(sc); // When using scanner as parameter your performance increases because you don't create new scanners every time if you use the scanner for different purposes or within a recursive function.
    System.out.printf("You have %(,.2f dollars%n", money); // Just an example of your output.
}

public static BigDecimal getBigDecimal(Scanner sc) {
    System.out.print("How much money do you have? ");
    if (sc.hasNextBigDecimal()) { // if a BigDecimal value is typed
        return sc.nextBigDecimal();
    }
    sc.next(); // else ignore input
    System.out.println("Please, type amount.");
    return getBigDecimal(sc); // recursion
}

For money you better use BigDecimal (believe me, or don't believe me). Further you can use BigDecimal functions to make your calculations.

https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial http://www.tutorialspoint.com/java/math/java_math_bigdecimal.htm

LowLevel
  • 1,085
  • 1
  • 13
  • 34