How many gumballs? How many kids? 80.5 6
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at KeepingMoreKidsQuiet.main(KeepingMoreKidsQuiet.java:12)
I get an InputMismatchException because keyboard.nextInt() expects me to type an int value, and 80.5 isn't an int value.
How many gumballs? How many kids? "80" "6"
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at KeepingMoreKidsQuiet.main(KeepingMoreKidsQuiet.java:12)
I get an InputMismatchException because keyboard.nextInt() expects me to type an int value, and "80", with quotation marks, isn't an int value. (In fact, "80" is a string of characters -- the digit character 8 followed by the digit character 0. More on that in Chapter 18.)
import java.util.Scanner;
public class AddingMachine {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
int firstNumber;
int secondNumber;
int sum;
System.out.print("Enter two numbers: ");
firstNumber = keyboard.nextInt();
secondNumber = keyboard.nextInt();
sum = firstNumber + secondNumber;
System.out.print("The sum is ");
System.out.print(sum);
System.out.println(".");
keyboard.close();
}
}
import java.util.Scanner;
public class Plumber {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
int hours;
int charge;
System.out.print("How many hours does the plumber work? ");
hours = keyboard.nextInt();
charge = 75 + hours * 125;
System.out.print("The plumber charges $");
System.out.print(charge);
System.out.println(".");
keyboard.close();
}
}
public class HowTall {
public static void main(String[] args) {
double height;
double inches;
height = 5.5;
inches = height * 12;
System.out.print("Barry is ");
System.out.print(inches);
System.out.println(" inches tall.");
}
}
import java.util.Scanner;
public class HowTall {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
double height;
double inches;
System.out.print("How tall are you? (Example: 5.5 for five and half feet) ");
height = keyboard.nextDouble();
inches = height * 12;
System.out.print("You're ");
System.out.print(inches);
System.out.println(" inches tall.");
keyboard.close();
}
}
import java.util.Scanner;
public class HowTall2 {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
int feet;
int inches;
int height;
System.out.print("How many feet? How many inches? ");
feet = keyboard.nextInt();
inches = keyboard.nextInt();
height = feet * 12 + inches;
System.out.print("You're ");
System.out.print(height);
System.out.println(" inches tall.");
keyboard.close();
}
}
import java.util.Scanner;
public class Anniversaries {
public static void main(String[] args) {
var keyboard = new Scanner(System.in);
int years;
System.out.print("How many years? ");
years = keyboard.nextInt();
System.out.print("Number of anniversaries: ");
System.out.println(years / 4);
keyboard.close();
}
}