import java.util.Scanner;
public class Main {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
var words = new String[5];
int i = 0;
try {
do {
words[i] = keyboard.next();
} while (!words[i++].equals("Quit"));
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
System.out.println("You can enter at most five words");
}
for (int j = 0; j < 5; j++) {
try {
System.out.println(words[j].length());
} catch (NullPointerException e) {
System.out.println(e.getMessage());
System.out.println("You could have entered up to five words");
}
}
keyboard.close();
}
}
public class OutOfRangeException extends RuntimeException {
public OutOfRangeException(String message) {
super("A value is out of range.\n" + message);
}
}
class NumberTooLargeException extends OutOfRangeException {
public NumberTooLargeException(String message) {
super("A value is too large.\n" + message);
}
}
import java.text.NumberFormat;
import java.util.Scanner;
import static java.lang.System.out;
public class InventoryD {
public static void main(String[] args) {
double boxPrice = 0.0;
int maxBoxes = 0;
var keyboard = new Scanner(System.in);
NumberFormat currency = NumberFormat.getCurrencyInstance();
try {
out.print("What's the price per box? ");
String boxPriceIn = keyboard.next();
boxPrice = Double.parseDouble(boxPriceIn);
if (boxPrice < 0.0){
throw new OutOfRangeException("Price per box cannot be negative.");
}
out.print("What's the maximum number of boxes? ");
String maxBoxesIn = keyboard.next();
maxBoxes = Integer.parseInt(maxBoxesIn);
if (maxBoxes < 0){
throw new OutOfRangeException("Maximum number of boxes cannot be negative.");
}
out.print("How many boxes do we have? ");
String numBoxesIn = keyboard.next();
int numBoxes = Integer.parseInt(numBoxesIn);
if (numBoxes < 0) {
throw new OutOfRangeException("You typed " + numBoxes +
". There's no such thing as a negative box.");
}
if (numBoxes > maxBoxes) {
throw new NumberTooLargeException(numBoxes +
" is larger than the maximum of " + maxBoxes);
}
out.print("The value is ");
out.println(currency.format(numBoxes * boxPrice));
} catch (NumberFormatException e) {
out.print(e.getMessage());
out.println(" ... Cannot interpret the input.");
} catch (OutOfRangeException e) {
out.println(e.getMessage());
} catch (Exception e) {
out.println(e.getMessage());
}
out.println("That's that.");
keyboard.close();
}
}