Back

import java.util.*;
public class example7
{


   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);

      String oneLine;
      StringTokenizer str;

      int x, y;

      System.out.println("Enter 2 integers on one line, to Exit enter one zero");

      try
      {
         oneLine = sc.nextLine();
         if (oneLine == null)
            return;


         while (true)
         {
            str = new StringTokenizer (oneLine);
            if (str.countTokens() == 1)
            {
               if (Integer.parseInt(str.nextToken())  == 0)
               {
                  return;
               }
               else
               {
                  System.out.println("Please enter two integers or one zero to exit 1");
                  oneLine = sc.nextLine();
               }
            }
            else if (str.countTokens() !=2)
            {
               System.out.println("Please enter two integers or one zero to exit 2");
               oneLine = sc.nextLine();
            }
            else
            {
               x = Integer.parseInt(str.nextToken());
               y = Integer.parseInt(str.nextToken());
               System.out.println("Total is: " + (y+x));
               System.out.println("Enter 2 integers on one line, to Exit enter one zero 3");
               oneLine = sc.nextLine();
            }
         }
      }

      catch (NumberFormatException e)
      {
         System.err.println("Please enter only integers");
         return;
      }


   }

}

Top