Question
I'm getting errors with these methods in my code but not sure what I need to change The Longest Word Length Method TO DO: 1.
I'm getting errors with these methods in my code but not sure what I need to change
The Longest Word Length Method
TO DO: 1. Write a method that returns length of the longest word in the given String using recursion (no loops). Hint: a Scanner may be helpful for finding word boundaries. After delimiting by space, use the following method on your String to remove punctuation .replaceAll("[^a-zA-Z]", "") If you use a Scanner, you will need a helper method to do the recursion on the Scanner object. When you run this method, your output should look like this:
Testing out the longestWordLength method The longest word in the following quote: Grit, one of the keys to success. The person who perseveres is the one who will surely win. Success does not come from giving up, it comes from believing in yourself and continuously working towards the realization of a worthy ideal. Do not ever give up on what you want most. You know what you truly want. Believe in your dreams and goals and take daily consistent action in order to make your dreams a reality. should be 12 -> 12 The longest word in the following quote: Try to be like the turtle at ease in your own shell. should be 6 -> 6
The Remove Duplicates Method
TO DO: 1. Write a method to remove consecutive duplicate characters from a String. If two or more consecutive duplicate characters have different cases, then the first letter should be kept.
When you run this method, your output should look like this:
Testing the dedupeChars method dedupeChars("a") should be a -> a dedupeChars("aa") should be a -> a dedupeChars("MiSsisSiPpi") should be MiSisiPi -> MiSisiPi dedupeChars("swimMmMming") should be swiming -> swiming
Code I have written for these two methods:
public static int longestWordLength(String words){ String [] s = words.split("\\words+"); int max = 0; for (String word : s) max = Math.max(max, word.length()); return max; }
public static String dedupeChars(String word){ StringBuilder sb = new StringBuilder(); for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); if (i == 0 || c != word.charAt(i - 1)) sb.append(c); } return sb.toString(); }
Errors I'm getting:
The longest word in the following quote: Grit, one of the keys to success. The person who perseveres is the one who will surely win. Success does not come from giving up, it comes from believing in yourself and continuously working towards the realization of a worthy ideal. Do not ever give up on what you want most. You know what you truly want. Believe in your dreams and goals and take daily consistent action in order to make your dreams a reality.
should be 12 -> 412
The longest word in the following quote: Try to be like the turtle at ease in your own shell.
should be 6 -> 54
Testing the dedupeChars method dedupeChars("a") should be a -> a dedupeChars("aa")
should be a -> a
dedupeChars("MiSsisSiPpi")
should be MiSisiPi -> MiSsisSiPpi
dedupeChars("swimMmMming")
should be swiming -> swimMmMming
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started