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!

1

4 Answers 4

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

    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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

5 Comments

our teacher doesn't want us to use BigDecimal so do you know how I can maybe throw an exception?
Why do you want to throw exception?
You can use if (scanner.hasNextDouble()) or you can get scanner.NextLine and parse it to double and catch NumberFormatException
You also can use scanner.hasNext(Pattern) // Regular Expressions
In a certain sense you are the boss of your application and you tell the user the way to interact with your application.
-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().

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.