
import java.util.Scanner;

public class StringExercises 
{
	private String s1;
	private String s2;

	public StringExercises()
	{ 
		s1 = "";
		s2 = "";
	}

	public StringExercises( String s1, String s2 )
	{
		this.s1 = s1;
		this.s2 = s2;
	}

	public void menu() 
	{
		Scanner scanner = new Scanner(System.in);

		System.out.println("\n(1) (Re-)Input the first string.");
		System.out.println("(2) (Re-)Input the second string.");
		System.out.println("(3) Compare whether the Strings have equivalent values.");
		System.out.println("(4) Compare the ascii difference between the two strings.");
		System.out.println("(5) Determine whether the two strings are the same object in memory.");
		System.out.println("(6) Determine the length of both strings.");
		System.out.println("(7) Find the character at a location within each string. The user will input the character position.");
	 	System.out.println("(8) Break the first string into a substring. The user will input the starting and ending positions of the substring.");
		System.out.println("(9) Combine the two strings into a single string, concating the two together.\n");
		System.out.print("Enter a Selection:  ");

		int choice = scanner.nextInt();

		if ( choice == 1 )
		{	System.out.print("(Re-)Input the first string : ");
			s1 = scanner.next(); 
			System.out.println("You have input : " + s1 );
      		}
		else if ( choice == 2 )
		{	System.out.print("(Re-)Input the second string : ");
			s2 = scanner.next(); 
			System.out.println("You have input : " + s2 );
      		}
		else if ( choice == 3 )
		{ 	System.out.print("The two strings");
			if( s1.equals(s2) == false )
				System.out.print(" DO NOT");
			System.out.println(" have equivalent values.");
      		}
		else if ( choice == 4 ) 
			System.out.println("Ascii difference between 2 strings is "+ s1.compareTo(s2) );
      		else if ( choice == 5 )
       		{	System.out.print("The two strings are");
			if(s1 != s2)
				System.out.print(" NOT");
       			System.out.println(" the same object in memory.");
      		}
		else if ( choice == 6 ) 
		{	System.out.println("The first string has length " + s1.length());
			System.out.println("The second string has length " + s2.length());
      		}
		else if ( choice == 7 )
		{	System.out.print("Please input the character position in string : ");
			int indx = scanner.nextInt();

			if( indx < s1.length() )
				System.out.println("The char at position "+indx+" in 1st string is : "+s1.charAt(indx));
			else System.out.println("Error: index out of 1st string range.");

			if( indx < s2.length() )
				System.out.println("The char at position "+indx+" in 2nd string is : "+s2.charAt(indx));
			else System.out.println("Error: index out of 2nd string range.");
      		}
		else if ( choice == 8 )
		{	System.out.print("Please input the begin index of substring : ");
			int begin = scanner.nextInt();
			System.out.print("Please input the end index of substring : ");
			int end = scanner.nextInt();

			if( begin >= 0 && end < s1.length() && begin <= end )
				System.out.println("The substring of first string is: " +s1.substring(begin,end)); 
			else System.out.println("Error: index out of range.");
      		}
		else if ( choice == 9 ) 
			System.out.println("The combined string is: " + s1.concat(s2));
		else
			System.out.println("Error: unknown selection.");

  	} // end menu method

	public String toString()
	{
		return "1st String = " + s1 + "\n2nd String = " + s2;
	}
}


