Detecting empty input (JAVA)
19,474
Solution 1
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);
}
}
}
Solution 2
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.
Author by
Jane Doe2
Updated on June 04, 2022Comments
-
Jane Doe2 almost 2 years
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!