1 FindAndReplace Complete the implementation of the (static) class method String
ID: 3786925 • Letter: 1
Question
1 FindAndReplace
Complete the implementation of the (static) class method String[] findAndReplace(String[] in, String[] what, String[] with) of the class Utils. The method returns a copy of the array in where each word occurring in the array what has been replaced by the word occurring at the corresponding position in the array with. The array designated by in must remain unchanged.
Later in the semester, we will learn about exceptions. Exceptions provide tools for handling error situations. In the meantime, the method findAndReplace returns null whenever one of the preconditions of the methods is violated:
In particular, the formal parameters cannot be null.
For all three arrays, none of the elements can be null.
The query and replacement arrays must be of the same length.
public class Utils {
/**
* Returns a copy of the array 'in' where each word occurring in the array
* 'what' has been replaced by the word occurring in the same position
* in the array 'with'.
*
* @param in an array of Strings;
* @param what an array of words to be replaced;
* @param with an array of replacement words;
* @return a new array idententical to 'in' except that all the occurrences of words
* found in 'what' have been replaced by the corresponding word from 'with'.
*/
public static String[] findAndReplace( String[] in, String[] what, String[] with ) {
String[] out = null; // The new array to be returned
boolean valid = true; // True if the pre-conditions are satistified
// Testing pre-conditions
if ( in == null || what == null || with == null ) {
valid = false;
} else {
//more or less 16 lines missing
}
if ( valid ) {
out = new String[ in.length ];
for ( int i=0; i<in.length; i++ ) {
//more or less 10 lines missing
}
}
// Returning a reference to the newly created array that
// contains the same entries as 'in' except that all the
// occurrences of words from 'what' have been replaced by
// their corresponding occurrence from 'with'.
return out;
}
}
Explanation / Answer
public class Sample1 {
public static String[] findAndReplace( String[] in, String[] what, String[] with ){
String out[] = null;
boolean valid = true;
//If one of the formal parameters is null it is invaid
if ( in == null || what == null || with == null ) {
valid = false;
}
else{
//For all three arrays, none of the elements can be null.
for(int i=0;i<in.length;i++){
if(in[i]==null){
valid = false;
break;
}
}
for(int i=0;i<what.length;i++){
if(what[i]==null){
valid = false;
break;
}
}
for(int i=0;i<with.length;i++){
if(with[i]==null){
valid = false;
break;
}
}
}
//If invalid return null without going further
if(valid){
//This concat string is the concatenation of the all the Strings in the array in seperated by space
String concat = "";
for(int i=0;i<in.length;i++){
concat = concat+" "+in[i];
}
//Now i have a string concat which is the concatenation of all the strings in 'in' array.
for(int i=0;i<what.length;i++){
//For every string , I'm replacing in the concat string.
concat = concat.trim().replaceAll(what[i], with[i]);
}
//Now concat string contains all the replaced strings. So, I'm collecting into out variable by splitting the concat string
out = concat.split(" ");
}
return out;
}
public static void main(String[] args) throws IOException {
//This is the data i created to test the program. It's working fine :-)
System.out.println(Arrays.toString(findAndReplace(new String[]{"my","name","is","prudhvi","raj","my","love","is","programming"}, new String[]{"my","raj"}, new String[]{"I","coder"})));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.