Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

scannerToString , which takes a Scanner as its parameter and returns a String. T

ID: 3871184 • Letter: S

Question

scannerToString, which takes a Scanner as its parameter and returns a String. The returned string consists of all the characters in the scanner in their original order, including the newlines. The last line (assuming there are any lines) should always end with a newline, even if it didn't in the original text. For example, scannerToString("This is a test") should return "This is a test ".

A convenient way to implement this method is to build up the result by reading one line at a time from the Scanner. For much improved efficiency when dealing with Scanners containing complete books, consider using a StringBuilder object to construct the result. You can look at my generateText method for an example of its use.  

chooseSubstring, which takes a String text, an int length, and a random number generator as its parameters. It should use the random number generator to return a randomly chosen substring of text that has the specified length. If length is either negative or greater than the length of text, the method should throw an IllegalArgumentException. For example, chooseSubstring("abcde", 4, new Random()) should return "abcd" about half the time and "bcde" about half the time.

A string of length n will contain n+1-m different substrings of length m, assuming n m. These substrings start at indexes ranging from 0 to n-m. There is a convenient method provided by the Random parameter that you can use to choose a random index.

getCharsThatFollowPattern, which takes a String text and a String pattern as parameters, and returns an ArrayList<Character>. The returned list should contain the character that follows each non-tail occurrence of the pattern in the text. (A non-tail occurrence of the pattern is one that is not at the very end of the text.) The length of the list must be the same as the number of non-tail occurrences of the pattern. The character stored at index n of the list must be the character that followed the nth non-tail occurrence of the pattern. For example, getCharsThatFollowPattern("abcabdabcab", "ab") should return the ArrayList ['c', 'd', 'c'].

To implement this method, use one of the indexOf methods provided by String objects.

pickCharThatFollowsPattern, which takes a String text, a String pattern, and a random number generator as parameters. It should randomly choose a non-tail occurrence of the pattern in the text, returning the character that immediately follows that occurrence of the pattern. If there are no non-tail occurrences of the pattern in the text, the method should throw a NoSuchElementException. For example, pickCharThatFollowsPattern("They are here", "he") should return 'y' or 'r' with equal probability.

You should use your getCharsThatFollowPattern method in your implementation of this method. If you don't, you'll be wasting a lot of time.

Explanation / Answer

public String scannerToString(Scanner s) {

String result = "";

while(s.hasNext()) {

result += s.nextLine() + " ";

}

return result;

}

String chooseSubstring(String text, int len, Random r) {

if(len < 0 || len > text.length()) {

throw new IllegalArgumentException("Invalid Length");

}

int startIndex = r.nextInt(text.length() - len + 1);

return text.substring(startIndex, startIndex + len);

}

ArrayList<Character> getCharsThatFollowPattern(String text, String pattern) {

ArrayList<Character> list = new ArrayList<>();

int start = 0;

while(true) {

int index = text.indexOf(pattern, start);

if(index == -1) {

break;

}

if(index + pattern.length() == text.length()) {

// tail sequence

break;

}

list.add(text.charAt(index + 1));

start = index + 1;

}

return list;

}

char pickCharThatFollowsPattern(String text, String pattern, Random r) {

ArrayList<Character> tailChars = getCharsThatFollowPattern(text, pattern);

return tailChars.get(r.nextInt(tailChars.size()));

}