Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Module : OO Programming Principles,
- Class Work : Week 2,
- Date : 21/01/2016,
- Author : Christopher Cullen
- Instructions:
- 1). ask the user to input a string and a character (read this in as a string).
- 2). using the startsWith method see if the string starts with the character.
- 3). using the endsWith method see if the string ends with the character.
- 4). Display the results on the screen. */
- public class Question2
- {
- public static void main(String[] args)
- {
- // New Scanner Object
- EasyScanner newScannerObject = new EasyScanner();
- // Input
- System.out.print("Enter in a string: ");
- String stringIn = newScannerObject.nextString();
- System.out.print("Enter in starting letter: ");
- String charFirst = newScannerObject.nextString();
- System.out.print("Enter in ending letter: ");
- String charLast = newScannerObject.nextString();
- // Declare a boolean
- boolean myVarOne, myVarTwo;
- // store the starts and endswith result in a var
- myVarOne = stringIn.startsWith(charFirst);
- myVarTwo = stringIn.endsWith(charLast);
- // compare statements.
- if(myVarOne)
- System.out.println("\n" + stringIn + " does start with " + charFirst.charAt(0));
- else
- System.out.println("\n" + stringIn + " does not start with " + charFirst.charAt(0));
- if(myVarTwo)
- System.out.println(stringIn + " ends with " + charLast.charAt(0));
- else
- System.out.println(stringIn + " does not end with " + charLast.charAt(0));
- }
- }
Add Comment
Please, Sign In to add comment