-
Notifications
You must be signed in to change notification settings - Fork 11
added all java files #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,122 @@ | ||
| public class StringManipulation implements StringManipulationInterface { | ||
|
|
||
| private String string; | ||
|
|
||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| return string; | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| this.string = string; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| if (string == null || string.isEmpty()) { | ||
| return 0; | ||
| } | ||
|
|
||
| String trimmedString = string.trim(); | ||
| if (trimmedString.isEmpty()) { | ||
| return 0; | ||
| } | ||
|
|
||
| // Split the string by whitespace to get individual words | ||
| String[] words = trimmedString.split("\\s+"); | ||
|
|
||
| return words.length; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| } | ||
| if (n <= 0) { | ||
| throw new IllegalArgumentException("Error: n is less than or equal to 0"); | ||
| } | ||
| else if (string == null) { | ||
| throw new NullPointerException("Error: String has not been initialized"); | ||
| } | ||
| else if (n > string.length()) { | ||
| throw new IndexOutOfBoundsException("Error: n exceeds string length"); | ||
| } | ||
|
|
||
|
|
||
| StringBuilder result = new StringBuilder(); | ||
| int count = 0; | ||
| for (int i = 0; i < string.length(); i++) { | ||
| char currentChar = string.charAt(i); | ||
| count++; | ||
| if (count % n == 0 && maintainSpacing) { | ||
| result.append(' '); | ||
| } | ||
| if (count % n != 0) { | ||
| result.append(currentChar); | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| return result.toString(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| if (string == null) { | ||
| throw new NullPointerException("Error: string is not yet initialized"); | ||
| } | ||
| String[] words = string.trim().split("\\s+"); | ||
| if(startWord <= 0 || endWord <= 0) { | ||
| throw new IllegalArgumentException("Error: start index and end index is less than 1"); | ||
| } | ||
|
|
||
| else if (startWord > endWord) { | ||
| throw new IndexOutOfBoundsException("Error: start index is larger than end"); | ||
| } | ||
|
|
||
| else if (endWord > words.length) { | ||
| throw new IndexOutOfBoundsException("Error: end index is larger than the string length"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you would be able to combine this else if statement with the one from line 79. This will help make the code shorter and will combine the same type of exception. |
||
| } | ||
|
|
||
| int substringsCount = endWord - startWord + 1; | ||
| String[] substrings = new String[substringsCount]; | ||
|
|
||
| int index = 0; | ||
| for (int i = startWord - 1; i < endWord; i++) { | ||
| substrings[index++] = words[i]; | ||
| } | ||
|
|
||
| return substrings; | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| } | ||
| if (string == null) { | ||
| throw new NullPointerException("Error: string is not yet initialized"); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| if (indices.length != string.length()) { | ||
| throw new IllegalArgumentException("Error: string length is not equal to indices length"); | ||
| } | ||
| for (int i = 0; i < indices.length - 1; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This outer nested for loop seems redundant. I'm not sure why you need to run the for loop for indices.length - 1. I believe you can just combine the 2 for loops into for (int i = 0; i < indices.length; i++) and then just run the exception in your if statement for if indices[i]< 0 or indices[i]>= string length |
||
| for (int j = i + 1; j < indices.length; j++) { | ||
| if (indices[i] == indices[j]) { | ||
| throw new IllegalArgumentException("Error: some indexes are not unique"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| char[] restored = new char[indices.length]; | ||
| for (int i = 0; i < indices.length; i++) { | ||
|
|
||
| restored[i] = string.charAt(indices[i]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can run this code in the original for loop at line 108 instead of writing a whole new for loop for this portion. |
||
| } | ||
| return new String(restored); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Null Pointer Exception error isn't required for this method. I'm pretty sure this is actually supposed to be an Illegal Argument Exception so I would modify this as part of that exception.