initialization
- the
actual initial values will
come from the user



decision (loop while we don't
have an integer in the input stream)

loop body.  Consume the bad
value and display it.  

update - Reprompt the user. The user provides the update value.





Another analogous loop structure
























this is simply an if statement.  No reason to loop.
If the user gives us a zero for operand two, we should
still carry out the operations that we can and guard
against failure if we can't.
import java.util.Scanner;
/**
 *  This class will demonstrate problems with bad input
 *
 *  @author Nancy Harris
 *  @version V3 - 10/13/2008
 */
public class BadInputV3
{
    /**
     * main prompts for two values and performs various operations on those operands
     *
     * @param args unused in this program
     */
    public static void main(String args[])
    {
        int         operand1, operand2;
        int         result;
        String    badValue;
        Scanner  keyboard;
               
        // instantiate our Scanner object
        keyboard = new Scanner(System.in);
       
        System.out.print("Enter two integers separated by a space: ");
       
        // must protect against ugly input
        // what if we want to loop until the user gives us something
        // appropriate?
       
        // while the person has not given us good data
        while(!keyboard.hasNextInt())
        {
            badValue = keyboard.nextLine(); // pull in both
            System.out.println("Bad value - " + badValue + ". Try again");
            System.out.println("Enter two integers separated by a space: ");
        }

        // we know we have a good int in the first position
        // if we get to this point
       
        operand1 = keyboard.nextInt();
       
        // now check the second       
        while(!keyboard.hasNextInt())
        {
            badValue = keyboard.next();
            System.out.println("Bad value - " + badValue + ". Try again");
            System.out.println("Enter an integer: ");
        }
       
        // we know we have a good int in the first position
        // if we get to this point
        operand2 = keyboard.nextInt();

        System.out.print("\n");
       
        // carry out operations
        result = operand1 + operand2;
        System.out.println(operand1 + " + " + operand2 + " = " + result);
       
        result = operand1 - operand2;
        System.out.println(operand1 + " - " + operand2 + " = " + result);
       
        result = operand1 * operand2;
        System.out.println(operand1 + " * " + operand2 + " = " + result);
       
        // must protect from 0 value still
           
        if (operand2 != 0)
        {
            result = operand1 / operand2;
            System.out.println(operand1 + " / " + operand2 + " = " + result);
       
            result = operand1 % operand2;
            System.out.println(operand1 + " % " + operand2 + " = " + result);
        }
        else
            System.out.println("Cannot divide by zero");
    }   
}