Advertisement
Guest User

StringIntro Fuckboi

a guest
Feb 12th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class StringsIntro {
  3.  
  4. public static void main(String[] args)
  5. {
  6. Scanner input = new Scanner(System.in);
  7.  
  8. String string1;
  9. String string2;
  10.  
  11. System.out.println("Please input your 1st string: ");
  12. string1 = input.next();
  13.  
  14. System.out.println("Please input your 2nd string:");
  15. string2 = input.next();
  16.  
  17. System.out.println("------------------------------");
  18.  
  19.  
  20. System.out.println("The length of String 1: " + string1.length());
  21. System.out.println("The length of String 2: " + string2.length());
  22. System.out.println("");
  23.  
  24. System.out.println("Letters 2-4 of String 1: " + string1.substring(1,4));
  25. System.out.println("Letters 2-4 of String 2: " + string2.substring(1,4));
  26.  
  27. System.out.println("The first occurrence of the 'l' in String 1 is position: " + string1.indexOf("l"));
  28. System.out.println("The first occurrence of the 'l' in String 2 is position: " + string2.indexOf("l"));
  29.  
  30. boolean eq;
  31. if(string1.equals(string2))
  32. eq = true;
  33. else
  34. eq = false;
  35. System.out.println("Are the 2 strings the same? : " + eq);
  36.  
  37. int comp = string1.compareTo(string2);
  38. if(comp == 0)
  39. System.out.println("String 1 is the same as String 2.");
  40. else
  41. {
  42. if(comp < 0)
  43. System.out.println("String 1 comes before String 2.");
  44. if(comp > 0)
  45. System.out.println("String 2 comes before String 1.");
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement