brainfrz

Untitled

Oct 22nd, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. /**************************************************************************************************
  2.  * Program Name:     Lab #8
  3.  * Author:           Terry Weiss
  4.  * Date Written:     October 27, 2015
  5.  * Course/Section:   CSC 111-003W
  6.  * Program Description:
  7.  *    This program uses a for-loop to prompt the User for 10 letters into an array, and then prints
  8.  * them back out.
  9.  **************************************************************************************************/
  10. package weisslab8;
  11.  
  12. import java.util.Scanner;
  13.  
  14. /*
  15.  * This program prompts the User for 10 letters. It stores them in an array, and then prints them
  16.  * back out.
  17.  *
  18.  * Input:
  19.  *     (String) letter – The letter entered
  20.  * Output:
  21.  *     (String) letter – The letter entered
  22.  * Additional Variables:
  23.  *     (int) LETTERS – The number of letters that will be prompted for
  24.  *     (String) letterArray – The array holding the letters
  25.  *     (String) literalNth – An array naming the position of each letter (“first”, “second”, etc)
  26.  *     (int) counter – The position in the array
  27.  *
  28.  * Initial Algorithm:
  29.  * For each letter, until the user has entered 10 letters:
  30.  *     Prompt for a letter, until it is a valid letter
  31.  *     Store the letter in an array
  32.  * For each letter in the array:
  33.  *     Display the letter
  34.  */
  35. public class WeissLab8
  36. {
  37.     /*
  38.      * This method uses a for-loop to prompt the User for 10 letters. It stores them in an array,
  39.      * and then prints them back out using a second for-loop.
  40.      *
  41.      * Refined Algorithm:
  42.      * FOR counter = 0 TO 9
  43.      *     DO
  44.      *         Prompt for a letter
  45.      *     LOOP WHILE letter isn’t 1 character OR letter isn’t a valid letter
  46.      *
  47.      *     Save letter to letterArray
  48.      * NEXT counter
  49.      *
  50.      * FOR counter = 0 TO 9
  51.      *     Display the current letter from letteryArray
  52.      * NEXT counter
  53.      */
  54.     public static void main( String[] args )
  55.     {
  56.         /*
  57.          * The number of letters that will be prompted for.
  58.          */
  59.         final int LETTERS = 10;
  60.  
  61.         /*
  62.          * Object storing the user's input.
  63.          */
  64.         Scanner user_input = new Scanner(System.in);
  65.  
  66.         /*
  67.          * The letter entered.
  68.          */
  69.         String letter;
  70.  
  71.         /*
  72.          * Holds the letters provided by the User.
  73.          */
  74.         String[] letterArray = new String[LETTERS];
  75.  
  76.         /*
  77.          * Labels for each letter's position in the array. ("first", "second", etc)
  78.          */
  79.         String[] literalNth = {
  80.             "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth",
  81.             "Ninth", "Tenth"
  82.         };
  83.  
  84.  
  85.  
  86.         for (int counter = 1; counter <= LETTERS; counter++)
  87.         {
  88.             do //while character isn't valid
  89.             {
  90.                 System.out.print("Please enter letter #" + counter + ": ");
  91.                 letter = user_input.nextLine();
  92.  
  93.                 if (letter.length() != 1 || !Character.isLetter(letter.charAt(0)))
  94.                 {
  95.                     System.out.print("That isn't a letter. ");
  96.                 } //end if isn't valid character
  97.                 else
  98.                 {
  99.                     letterArray[counter - 1] = letter;
  100.                 } //end else is valid character
  101.             } while (letter.length() != 1 || !Character.isLetter(letter.charAt(0)));
  102.         } //end for each letter to be prompted
  103.  
  104.  
  105.         for (int counter = 0; counter < LETTERS; counter++)
  106.         {
  107.             System.out.println(literalNth[counter] + " letter: " + letterArray[counter]);
  108.         } //end for each letter prompted
  109.     } //end method main
  110. } //end class WeissLab8
Advertisement
Add Comment
Please, Sign In to add comment