For this lab we will review string manipulation. You will be writing a program w
ID: 3785155 • Letter: F
Question
For this lab we will review string manipulation. You will be writing a program which will prompt the user for a string in a specific format. You will then parse the input string and print in a canonicalized format. The string is expected to consist three 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 enters an empty or just white spaces. The first and the second parts are not valid range 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 tinfinity many semicolons in the string (more than two). Find this lab work, it is OK if you program crashes when calling Integer.parslent 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 are shown below.Explanation / Answer
Hi
PFB the Class.Please comment for any queries/feedback.
import java.util.Scanner;
public class StringTester {
public static void main(String [] args){
int beginIndex=0;
int endIndex=0;
String beginIndexStr="";
String endIndexStr="";
String partsArr[] = new String[3];
boolean beginIdexSet =false;
Scanner scanner= new Scanner(System.in);
System.out.println("Please Enter a String: ");
String inputStr = scanner.nextLine();
String inputStrVal = inputStr;
int semiColonCount =0;
boolean processString =false;
int endIndexCounter=0;
boolean isExit =false;
if("".equalsIgnoreCase(inputStrVal) || " ".equalsIgnoreCase(inputStrVal)){
System.out.println("Invalid input!");
}
else{
for(int i=0;i<inputStr.length();i++){
if(i==inputStr.indexOf(';')){
inputStrVal= inputStr.substring(i+1);
for(int k=0;k<inputStrVal.length();k++){
if(k==inputStrVal.indexOf(';')){
inputStrVal = inputStrVal.substring(k+1);
if(inputStrVal.contains(";")){
System.out.println("Too many semicolons!");
}
isExit =true;
break;
}
else{
beginIdexSet =true;
endIndexStr=endIndexStr+inputStrVal.charAt(k);
}
}
if(isExit){
break;
}
}
if(!beginIdexSet){
beginIndexStr=beginIndexStr+inputStrVal.charAt(i);
}
}
if(beginIdexSet){
beginIndex = new Integer(beginIndexStr);
endIndex = new Integer(endIndexStr);
String[] partsArray =inputStr.split(";");
String thirdPart =partsArray[2];
if(beginIndex<0 || (endIndex>thirdPart.length()) || (beginIndex>endIndex) ){
System.out.println("Invalid Index!");
}
else{
String actualValue= thirdPart.substring(beginIndex,beginIndex+endIndex);
System.out.println(inputStr+" --> "+actualValue);
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.