Advertisement
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). Create two object of the String class.
- 2). Ask the user to enter two String values.
- 3). find out the length of the both string using the length method.
- 4). convert both strings to uppercase and lowercase.
- 5). use the compareTo method to see which string comes first in the alphabet, displaying appropriate messages on the outcome.
- 6). use the charAt method to see what value is at the 4 position in the first string and what value is at the 2 position in the second string, displaying appropriate messages on the outcome.
- 7). use the equals method to see if the two strings entered are equal, displaying appropriate messages on the outcome.
- 8). use the concat method to concatenate the two strings together, displaying appropriate messages on the outcome.
- 9). use the equalsIgnoreCase method to see if the two strings entered are equal, while ignoring the case of the strings, displaying appropriate messages on the outcome.
- 10). use the trim method so it will ignore any spaces in the string and then display the trimmed string to the screen. */
- public class Question1
- {
- public static void main(String[] args)
- {
- // New Scanner Object
- EasyScanner newScannerObject = new EasyScanner();
- // Enter and display strings.
- System.out.print("Enter in a string: ");
- String stringOne = newScannerObject.nextString();
- // String stringOne = EasyScanner.nextString();
- System.out.print("Enter in a second string: ");
- String stringTwo = newScannerObject.nextString();
- // String stringTwo = EasyScanner.nextString();
- // Display the length.
- int strLen = stringOne.length();
- int strLenTwo = stringTwo.length();
- System.out.println("\nThe length of the first string is: " + strLen + "\nThe length of the first string is: " + strLenTwo);
- // Change to Uppercase and lowercase
- System.out.println("\n" + stringOne + " to uppercase is: " + stringOne.toUpperCase() + "\n" + stringTwo + " to uppercase is: " + stringTwo.toUpperCase());
- System.out.println("\n" + stringOne + " to lowercase is: " + stringOne.toLowerCase() + "\n" + stringTwo + " to lowercase is: " + stringTwo.toLowerCase() + "\n");
- //Check order using compareTo()
- int result = stringOne.compareTo( stringTwo );
- if(result < 0)
- System.out.println(stringOne + " comes before " + stringTwo + " in the alaphabet");
- else if(result > 0)
- System.out.println(stringTwo + " comes before " + stringOne + " in the alaphabet");
- else if(result == 0)
- System.out.println(stringTwo + " is the same as " + stringOne );
- // get the char at position 4 and 2.
- if(stringOne.length() > 4 && stringTwo.length() > 2)
- System.out.println("\nThe character at position 4 in the " + stringOne + " is " + stringOne.charAt(3) + "\nThe character at position 2 in the " + stringTwo + " is " + stringTwo.charAt(1));
- else
- System.out.println("\nError: String is too short, please enter a string that has more than four characters");
- // Check strings are equal
- if(stringOne.equals(stringTwo))
- System.out.println("\n" + stringOne + " is the same as " + stringTwo);
- else if(!stringOne.equals(stringTwo))
- System.out.println("\n" + stringOne + " is NOT the same as " + stringTwo);
- // Concatinate Strings (NOT a . like in PHP)
- System.out.println("\nThe strings put together are " + stringOne.concat(stringTwo));
- // equalsIgnoreCase
- boolean myValue = stringOne.equals(stringTwo);
- if(myValue == false)
- System.out.println("\n" + stringOne + " is NOT same as " + stringTwo);
- else
- System.out.println("\n" + stringOne + " is the same as " + stringTwo);
- // Trim
- System.out.println("\nWith no leading or trailing spaces the first string is " + stringOne.trim() + "\nWith no leading or trailing spaces the second string is " + stringTwo.trim() );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement