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

String Manipulation - Wish to resolve without having to use the split method. Fo

ID: 3786994 • Letter: S

Question

String Manipulation

- Wish to resolve without having to use the split method.

For thin lab we will review string manipulation. You will be writing a program which will prompt the user for a sting in a specific format. You will then parse the input string and print it in a canonicalized format. The string is expected to consist parts separated by semicolon. The first and the second parts will be both integer. The third part will be a string. The first and second parts represents the indexes of the first and last character of a substring that your program will extract from the third part and print the answer to console. The input string format will be of the form;; your program must give an error message whenever a string is invalid. It should give a specific error message in the following three cases: If a user an empty or just white spaces. The first and the second parts are not valid in the third part. -if the beginIndex is negative OR -endindex is larger than the length of third part string, OR -beginIndex is larger than endindex. There are too many semicolons in the sting(more than two). For this lab work, it is OK if your program crashes when calling integer parseint and the argument is not a valid integer. You will need to use (at least) the indexof (the overloaded version) and substring methods. Some sample inputs and their outputs are shown below.

Explanation / Answer

File StringParser.java

import java.util.Scanner;

public class StringParser {

   public static void main(String[] args) {

       System.out.print("Enter the input string: ");
       Scanner sc = new Scanner(System.in);
       sc.useDelimiter(System.lineSeparator()); //Set scanner to take new line character as delimiter
       String string = sc.next(); // Read input
       if (string == null || string.isEmpty()) { // Empty check
           System.out.println("String is empty");
       } else if (!string.contains(";") || string.indexOf(";") == string.lastIndexOf(";")) { // Check if two semicolons are present
           System.out.println("String does not contain 2 semicolons");
       } else {
           String firstPart = string.substring(0, string.indexOf(";")); // extract first part
           String secondPart = string.substring(string.indexOf(";") + 1,
                   string.lastIndexOf(";")); // extract second part
           String thirdPart = string.substring(string.lastIndexOf(";") + 1); // extract third part
           int beginIndex = 0;
           int endIndex = 0;
           if (secondPart.contains(";")) { // Other validations
               System.out.println("Too many semicolons");
           } else if ((beginIndex = Integer.parseInt(firstPart)) < 0) {
               System.out.println("Invalid Index!");
           } else if ((endIndex = Integer.parseInt(secondPart)) > thirdPart
                   .length()) {
               System.out.println("Invalid Index!");
           } else if (beginIndex > endIndex) {
               System.out.println("Invalid Index!");

           } else {
               System.out.println(thirdPart.substring(beginIndex, endIndex)); // Print result
           }
       }

       sc.close();

   }

}