Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 132 additions & 11 deletions src/main/java/StringManipulation.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,154 @@
public class StringManipulation implements StringManipulationInterface {
import java.util.ArrayList;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very weird that I encountered this too but only after the build was semi-successful.

I did not get the same error until I could actually build and have found that some of my test cases failed as well.

Good catch.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alvin, so I seemed to have misunderstood my own solution to the Travis build fail issue. The fundamental problem was my mishandling of the commit & add commands while commenting in/out the import statements. So, if I'm not mistaken again, StringManipulation() does in fact need the import statement for ArrayList and StringManipulationTest() should only have the original import statements from the skeleton, no others.

After making sure this was the case, my build passed without issues (and without my panic), thank goodness.

In your case, I agree that your build had failed because, like you said, some of your test cases did not pass, and not because of import statement issues like me.


public class StringManipulation implements StringManipulationInterface {

private String string;
private int count;

@Override
public String getString() {
return null;
return string;
}

@Override
public void setString(String string) {
this.string = string;
}

@Override
public int count() {
return 0;
// initialize word count to 0
count = 0;

// if statement to check if string is null or empty
if (string == null || string.length() == 0) {
return count;
}

// split the string into a string array, delimiting by one or more spaces
String[] wordArray = this.string.split("\\s+");

count = wordArray.length;

return count;
}

@Override
public String removeNthCharacter(int n, boolean maintainSpacing) {
return null;
public String removeNthCharacter(int n, boolean maintainSpacing) {
if (n > string.length()) {
throw new IndexOutOfBoundsException();
} else if (n <= 0) {
throw new IllegalArgumentException();
}

// initialize an ArrayList of Characters
ArrayList<Character> charArray = new ArrayList<Character>();

// iterate through string and insert each Character in the ArrayList
for (int i = 0; i < string.length();i ++) {
charArray.add(string.charAt(i));
}

// determine the highest Nth index less than or equal to the length of this.string
int limit = string.length() / n;

// initialize an int array to hold all the indexes to replace
int[] indexesToReplace = new int[limit];

// assign each Nth index to an element in the int array
for (int i = 1, j = 0; i <= limit; i++, j++) {
indexesToReplace[j] = n * i;
}

//System.out.println("indexes to replace: " + Arrays.toString(indexesToReplace));

// iterate through the ArrayList and replace the Character with space or delete
for (int i = indexesToReplace.length - 1; i >= 0; i--) {
int index = indexesToReplace[i] - 1;

if (maintainSpacing == true) {
charArray.set(index, ' ');
}

if (maintainSpacing == false) {
charArray.remove(index);
}
}

// build the string from the ArrayList and return to main
StringBuilder stringBuilder = new StringBuilder();
for (char c : charArray) {
stringBuilder.append(c);
}

return stringBuilder.toString();
}

@Override
public String[] getSubStrings(int startWord, int endWord) {
return null;
public String[] getSubStrings(int startWord, int endWord){
// exception: If either "startWord" or "endWord" are invalid
if (startWord <= 0 || endWord <= 0 || startWord > endWord) {
throw new IllegalArgumentException();
}

// exception: If the string has less than "endWord" words in it
if (count() < endWord) {
throw new IndexOutOfBoundsException();
}

// split the words into a String array, delimited by one or more spaces
String[] arr = this.string.split("\\s+");

// intialize the return array with size of the number of words to keep
String[] finalArray = new String[endWord - startWord + 1];

// iterate through the array of all words, if the index falls inside the
// range passed in, then put that word in the new array
for (int i = 0, j = 0; i <= arr.length; i++) {
if (i >= startWord && i <= endWord) {
finalArray[j] = arr[i - 1];
j++;
}
}

return finalArray;
}

@Override
public String restoreString(int[] indices) {
return null;
public String restoreString(int[] indices){
// exception: if not s.length == indices.length == n
if (string.length() != indices.length) {
throw new IllegalArgumentException();
}

// exception: if indices[i] < 0 or indices[i] > string length
for (int i = 0 ; i < indices.length; i++) {
if (indices[i] < 0 || indices[i] >= string.length()) {
throw new IndexOutOfBoundsException();
}
}

// initialize an ArrayList for the final string to be returned
ArrayList<Character> finalWord = new ArrayList<>();

// fill the ArrayList with characters from the original string
for (char c : string.toCharArray()) {
finalWord.add(c);
}

// iterate through both the indices parameter and the string characters
// to load the final array with shuffled characters
for (int i = 0; i < string.length(); i++) {
finalWord.set(indices[i], string.charAt(i));
}

// build the string from the ArrayList and return to main
StringBuilder stringBuilder = new StringBuilder();
for (char c : finalWord) {
stringBuilder.append(c);
}

return stringBuilder.toString();
}


}

Loading