Using the Wrapper Classes And Scanner
You will write two programs. The first one is called Adder.java
.
Here I show a few runs of it. We use the Scanner
class to get
user input. You can probably do the whole thing in just a main
method. You will find Integer
methods handy. Use
yesterday's class as a "boneyard" for useful ideas.
unix> java Adder.java Welcome to the adder. To end, enter Q or q: Enter your first integer: 45 21 88 Q The total you entered amounted to 154. In binary this is 0b10011010. In octal this is 0o232. In hex this is 0x9a.
Now see this user misbehavior.
unix> java Adder Welcome to the adder. To end, enter Q or q: Enter your first integer: 34 21 quagmire Exception in thread "main" java.lang.NumberFormatException: For input string: "quagmire" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at Adder.main(Adder.java:31) (base) MAC:Tue Oct 27:08:09:adder
Uh oh. This is bad. Our program crashes whhen the user makes a botch of things.
We are going to make the program more bullet-proof. We are going to create
a new program AdderCheck.java
that exhibits these behaviors.
Here the user behaves himself.
unix> java AdderCheck Welcome to the adder. To end, enter Q or q: Enter an integer: 45 Enter an integer: 33 Enter an integer: 21 Enter an integer: 32 Enter an integer: q The total you entered amounted to 131. In binary this is 0b10000011. In octal this is 0o203. In hex this is 0x83.
Here the user does expected dumb stuff and we protect him from his arrant stupidity.
unix> java AdderCheck Welcome to the adder. To end, enter Q or q: Enter an integer: 5 Enter an integer: 9 Enter an integer: quack You entered an invalid integer; try again: boohah You entered an invalid integer; try again: 34 Enter an integer: 78 Enter an integer: q The total you entered amounted to 126. In binary this is 0b1111110. In octal this is 0o176. In hex this is 0x7e.
Notice how the application "nags" the user until he does the right thing. It will nag at every integer input if necessary.
Notice that surrounding whitespace is ignored and it does not trigger nagging.
java AdderCheck Welcome to the adder. To end, enter Q or q: Enter an integer: 45 Enter an integer: 35 Enter an integer: 21 Enter an integer: q The total you entered amounted to 101. In binary this is 0b1100101. In octal this is 0o145. In hex this is 0x65.
I will supply a suggested outline for a program here, which you may blithely disregard if you wish. If you do use it, stub your methods in so it compiles right out of the box.
import java.util.Scanner;
public class AdderCheck
{
//do this so the scanner is visible in all methods.
private static final Scanner scammer;
static
{
scammer = new Scanner(System.in);
}
private static String getIntegerString()
{
//get a string from the user
//do the nagging here.
//NB: get rid of whitespace on either side.
}
private static boolean isValidIntegerString(String n)
{
//check if n a valid integer string (optional
//+ or - followed by numbers
}
public static void main(String[] args)
{
//run the loop that gets all of the valid numbers
//adds them, and does the output routine at the end.
}
}