Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a complete java program that asks the user to enter a string S and two num

ID: 3804826 • Letter: W

Question

Write a complete java program that asks the user to enter a string S and two numbers x and y. The program should print the entered string and a substring of the original string starting from index x and ending with index y. The following is a sample run of the program, user input is underlined: Keep in mind that your program will be marked on accuracy and clarity and you should correctly implement the following components: Import of any required Java libraries. The structure of the class and the main method. Input statements to get the string S, numbers x and y from the user and process them. Appropriate output statements to interact with the user and display the required result.

Explanation / Answer

import java.util.Scanner;


public class SubString {

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String inputString;
System.out.print("Enter a String : ");
inputString = sc.nextLine();
int first, last;
System.out.print("Enter first index : ");
first = sc.nextInt();
  
System.out.print("Enter last index : ");
last = sc.nextInt();
  
// checking for error condition and handling it
if (last > inputString.length() - 1)
{
last = inputString.length()-1;
}
  
System.out.println("Your original String was : " + inputString);
String substring = inputString.substring(first, last+1);
System.out.println("Your substring is : " + substring);
sc.close();
}
}

// Sample run

$ java SubString
Enter a String : IT401 Midterm Exam
Enter first index : 2
Enter last index : 7
Your original String was : IT401 Midterm Exam
Your substring is : 401 Mi

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote