irishstorm

Question2.java

Jan 31st, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. /*  Module : OO Programming Principles,
  2.     Class Work : Week 2,
  3.     Date : 21/01/2016,
  4.     Author : Christopher Cullen          
  5.    
  6.     Instructions:
  7.     1). ask the user to input a string and a character (read this in as a string).
  8.     2). using the startsWith method see if the string starts with the character.
  9.     3). using the endsWith method see if the string ends with the character.
  10.     4). Display the results on the screen.                                      */
  11.  
  12. public class Question2
  13. {
  14.     public static void main(String[] args)
  15.     {
  16.         //  New Scanner Object
  17.         EasyScanner newScannerObject = new EasyScanner();
  18.        
  19.  
  20.         //  Input
  21.         System.out.print("Enter in a string: ");
  22.         String stringIn = newScannerObject.nextString();
  23.  
  24.         System.out.print("Enter in starting letter: ");
  25.         String charFirst = newScannerObject.nextString();
  26.  
  27.         System.out.print("Enter in ending letter: ");
  28.         String charLast = newScannerObject.nextString();
  29.  
  30.  
  31.         //  Declare a boolean
  32.         boolean myVarOne, myVarTwo;
  33.  
  34.         // store the starts and endswith result in a var
  35.         myVarOne = stringIn.startsWith(charFirst);
  36.         myVarTwo = stringIn.endsWith(charLast);
  37.  
  38.         // compare statements.
  39.         if(myVarOne)
  40.             System.out.println("\n" + stringIn + " does start with " + charFirst.charAt(0));
  41.         else
  42.             System.out.println("\n" + stringIn + " does not start with " + charFirst.charAt(0));
  43.  
  44.         if(myVarTwo)
  45.             System.out.println(stringIn + " ends with " + charLast.charAt(0));
  46.         else
  47.             System.out.println(stringIn + " does not end with " + charLast.charAt(0));
  48.     }
  49. }
Add Comment
Please, Sign In to add comment