Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class StringsIntro {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- String string1;
- String string2;
- System.out.println("Please input your 1st string: ");
- string1 = input.next();
- System.out.println("Please input your 2nd string:");
- string2 = input.next();
- System.out.println("------------------------------");
- System.out.println("The length of String 1: " + string1.length());
- System.out.println("The length of String 2: " + string2.length());
- System.out.println("");
- System.out.println("Letters 2-4 of String 1: " + string1.substring(1,4));
- System.out.println("Letters 2-4 of String 2: " + string2.substring(1,4));
- System.out.println("The first occurrence of the 'l' in String 1 is position: " + string1.indexOf("l"));
- System.out.println("The first occurrence of the 'l' in String 2 is position: " + string2.indexOf("l"));
- boolean eq;
- if(string1.equals(string2))
- eq = true;
- else
- eq = false;
- System.out.println("Are the 2 strings the same? : " + eq);
- int comp = string1.compareTo(string2);
- if(comp == 0)
- System.out.println("String 1 is the same as String 2.");
- else
- {
- if(comp < 0)
- System.out.println("String 1 comes before String 2.");
- if(comp > 0)
- System.out.println("String 2 comes before String 1.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement