
/**
 * It demonstrates input facilities using tio class. 
 * This example illustrates better exception handling mechanism, 
 * so that the input shall not be repeated from the very beginning
 *
 * @author Ageenko
 */

import tio.*;

public class Test6 {

    public static int readInt() {
        boolean ok = false;
        int value = 0;
        do {
           try {
                value = Console.in.readInt();
                ok = true;
           } catch (NumberFormatException e) {
                Console.out.println(e.getMessage() + " cannot convert to integer");
                Console.out.println("Repeat from this point, please");
                Console.in.readLine();
           }
        } while (!ok);
        Console.out.println(value + " is acepted.");
        return value;
    }


    public static void main(String[] args) {
        int a = 0, b = 0;

        try {

            Console.out.printfln("Please input two integeres:");
            a = readInt();
            b = readInt();

            Console.out.setWidth(20); Console.out.setJustify(FormattedWriter.RIGHT);

            Console.out.printf("The sum is: ");         // formatted output
            Console.out.println(a+b);                   // non-formatted output 

            Console.out.printf("The difference is: ");  // formatted output
            Console.out.println(a-b);                   // non-formatted output 

            Console.out.println("All done.");

        } catch (tio.ReadException e) {
        
            Console.out.println("Error reading all the data. Terminated.");
        }

        System.exit(0);
    }
}
