Advertisement
irishstorm

Question1.java

Jan 31st, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 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). Create two object of the String class.
  8.     2). Ask the user to enter two String values.
  9.     3). find out the length of the both string using the length method.
  10.     4). convert both strings to uppercase and lowercase.
  11.     5). use the compareTo method to see which string comes first in the alphabet, displaying appropriate messages on the outcome.
  12.     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.
  13.     7). use the equals method to see if the two strings entered are equal, displaying appropriate messages on the outcome.
  14.     8). use the concat method to concatenate the two strings together, displaying appropriate messages on the outcome.
  15.     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.
  16.     10). use the trim method so it will ignore any spaces in the string and then display the trimmed string to the screen.                                                                      */
  17.  
  18. public class Question1
  19. {
  20.     public static void main(String[] args)
  21.     {
  22.         //  New Scanner Object
  23.         EasyScanner newScannerObject = new EasyScanner();
  24.  
  25.  
  26.         // Enter and display strings.
  27.         System.out.print("Enter in a string: ");
  28.         String stringOne = newScannerObject.nextString();
  29.         // String stringOne = EasyScanner.nextString();
  30.  
  31.         System.out.print("Enter in a second string: ");
  32.         String stringTwo = newScannerObject.nextString();
  33.         // String stringTwo = EasyScanner.nextString();
  34.  
  35.  
  36.  
  37.         // Display the length.
  38.         int strLen = stringOne.length();
  39.         int strLenTwo = stringTwo.length();
  40.         System.out.println("\nThe length of the first string is: " + strLen + "\nThe length of the first string is: " + strLenTwo);
  41.  
  42.  
  43.  
  44.         // Change to Uppercase and lowercase
  45.         System.out.println("\n" + stringOne + " to uppercase is: " + stringOne.toUpperCase() + "\n" + stringTwo + " to uppercase is: " + stringTwo.toUpperCase());
  46.         System.out.println("\n" + stringOne + " to lowercase is: " + stringOne.toLowerCase() + "\n" + stringTwo + " to lowercase is: " + stringTwo.toLowerCase() + "\n");
  47.    
  48.        
  49.  
  50.         //Check order using compareTo()
  51.         int result = stringOne.compareTo( stringTwo );
  52.         if(result < 0)
  53.             System.out.println(stringOne + " comes before " + stringTwo + " in the alaphabet");
  54.         else if(result > 0)
  55.             System.out.println(stringTwo + " comes before " + stringOne + " in the alaphabet");
  56.         else if(result == 0)
  57.             System.out.println(stringTwo + " is the same as " + stringOne );
  58.  
  59.  
  60.  
  61.         //  get the char at position 4 and 2.
  62.         if(stringOne.length() > 4 && stringTwo.length() > 2)
  63.             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));
  64.         else
  65.             System.out.println("\nError: String is too short, please enter a string that has more than four characters");
  66.  
  67.    
  68.  
  69.         //  Check strings are equal
  70.         if(stringOne.equals(stringTwo))
  71.             System.out.println("\n" + stringOne + " is the same as " + stringTwo);
  72.         else if(!stringOne.equals(stringTwo))
  73.             System.out.println("\n" + stringOne + " is NOT the same as " + stringTwo);
  74.  
  75.  
  76.  
  77.         // Concatinate Strings (NOT a . like in PHP)
  78.         System.out.println("\nThe strings put together are " + stringOne.concat(stringTwo));
  79.  
  80.  
  81.  
  82.         //  equalsIgnoreCase
  83.         boolean myValue = stringOne.equals(stringTwo);
  84.         if(myValue == false)
  85.             System.out.println("\n" + stringOne + " is NOT same as " + stringTwo);
  86.         else
  87.             System.out.println("\n" + stringOne + " is the same as " + stringTwo);
  88.  
  89.  
  90.  
  91.         // Trim
  92.         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() );
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement