Source of IOTesting.java


  1: import java.util.Scanner;

  3: /**
  4:  * IOTesting -- a program to demonstrate console input/output
  5:  *
  6:  * @author Mark Young (A00000000)
  7:  * @version 1.10 2010-05-17
  8:  */
  9: public class IOTesting {

 11:     public static void main(String[] args) {
 12:         // introduce myself
 13:         System.out.print("\n\n\n\n"
 14:                 + "IOTesting -- a program to demonstrate console input/output\n"
 15:                 + "\n\n\n\n");

 17:         // get ready for integer IO
 18:         int n1, n2;
 19:         Scanner keyboard = new Scanner(System.in);

 21:         // read and sum a first pair of numbers (allows anticipation)
 22:         System.out.print("Enter a whole number: ");
 23:         n1 = keyboard.nextInt();
 24:         System.out.print("Enter another: ");
 25:         n2 = keyboard.nextInt();
 26:         System.out.println("The sum of " + n1 + " and " + n2
 27:                 + " is " + (n1 + n2) + ".");

 29:         // tidy up a bit
 30:         keyboard.nextLine();
 31:         System.out.println();

 33:         // show that anticipation works
 34:         System.out.println("Let's try that again -- "
 35:                 + "but put both numbers on the first line....");
 36:         System.out.print("Enter a whole number: ");
 37:         n1 = keyboard.nextInt();
 38:         System.out.print("Enter another: ");
 39:         n2 = keyboard.nextInt();
 40:         System.out.println("The sum of " + n1 + " and " + n2
 41:                 + " is " + (n1 + n2) + ".");

 43:         // tidy up a bit
 44:         keyboard.nextLine();
 45:         System.out.println();

 47:         // read and sum a second pair of numbers (prevents anticipation)
 48:         System.out.println("And one more time "
 49:                 + "with two numbers on the first line....");
 50:         System.out.print("Enter a whole number: ");
 51:         n1 = keyboard.nextInt();
 52:         keyboard.nextLine();
 53:         System.out.print("Enter another: ");
 54:         n2 = keyboard.nextInt();
 55:         keyboard.nextLine();
 56:         System.out.println("The sum of " + n1 + " and " + n2
 57:                 + " is " + (n1 + n2) + ".");

 59:         // tidy up a bit (less to do here!)
 60:         System.out.println();

 62:         // now read and sum a pair of doubles
 63:         double x, y;
 64:         System.out.print("Enter two numbers with decimals: ");
 65:         x = keyboard.nextDouble();
 66:         y = keyboard.nextDouble();
 67:         keyboard.nextLine();    // tidy up as we go along!
 68:         System.out.println("The sum of " + x + " and " + y
 69:                 + " is " + (x + y) + ".");
 70:         System.out.println();

 72:         // now read a couple of words
 73:         String w1, w2;
 74:         System.out.print("Enter two words: ");
 75:         w1 = keyboard.next();
 76:         w2 = keyboard.next();
 77:         keyboard.nextLine();    // tidying up as we go along -- a good idea
 78:         System.out.println("The words you entered were \""
 79:                 + w1 + "\" and \"" + w2 + "\".");
 80:         System.out.println();

 82:         // read and echo back a whole line of text
 83:         String line;
 84:         System.out.println("Next enter a whole line of text.  "
 85:                 + "Press the ENTER key when you're done:");
 86:         line = keyboard.nextLine();
 87:         System.out.println("The line you entered was \""
 88:                 + line + "\".\n\n");

 90:         // lastly we pause
 91:         System.out.print("And we're done!  "
 92:                 + "Press the ENTER key to finish the program. ");
 93:         keyboard.nextLine();
 94:         System.out.println();
 95:     }

 97: }