Can you help me incorporate the StdIn.readAllStrings() method rather than the wa
ID: 3875974 • Letter: C
Question
Can you help me incorporate the StdIn.readAllStrings() method rather than the way I currently have it to use StdIn.readString()? The program should read in a text file with various strings (some duplicated) and print out the number of times a string appears in the file. Right now, the program is printing out the number of times a string appears in each individual line in a file.
Thanks!
package program2;
import algs31.SequentialSearchST;
import algs32.BST;
import stdlib.*;
public class TimeSymbolTables {
public static void main(String[] args) {
StdIn.fromFile("data/tinyTale.txt");
int count;
SequentialSearchST wordCountSSST = new SequentialSearchST<>();
Stopwatch swSSST = new Stopwatch();
while (!StdIn.isEmpty()) {
String key = StdIn.readString();
if (wordCountSSST.get(key) == null) {
wordCountSSST.put(key, 1);
}
if (wordCountSSST.get(key) != null) {
count = wordCountSSST.get(key) +1;
wordCountSSST.put(key, count);
}
StdOut.println("[" + key + "," + wordCountSSST.get(key) + "]");
}
StdOut.println("The symbol table was filled in " + swSSST.elapsedTime() + " using the SequentialSearchST program class.");
}
Here are the mothods from the stdlib class to read in data from a file:
/**
* Read and return the next line.
*/
public String readLine() {
String line;
try { line = scanner.nextLine(); }
catch (Exception e) { line = null; }
return line;
}
/**
* Read and return the next string.
*/
public String readString() {
return scanner.next();
}
private Scanner scanner;
/* * begin: section (1 of 2) of code duplicated from In to StdIn */
// assume Unicode UTF-8 encoding
private static final String charsetName = "UTF-8";
// assume language = English, country = US for consistency with System.out.
private static final java.util.Locale usLocale =
new java.util.Locale("en", "US");
// the default token separator; we maintain the invariant that this value
// is held by the scanner's delimiter between calls
private static final Pattern WHITESPACE_PATTERN
= Pattern.compile("\p{javaWhitespace}+");
// makes whitespace characters significant
private static final Pattern EMPTY_PATTERN
= Pattern.compile("");
// used to read the entire input. source:
// http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
private static final Pattern EVERYTHING_PATTERN
= Pattern.compile("\A");
Explanation / Answer
Add the following code into your program:
public static void savePosition() { //to keep track of the string tokens it is used
try {
position = scanner.match().end();
}
catch (IllegalStateException e) {
position = 0; // nothing has been read yet
}
}
public static String[] readAllStrings() {
// we could use readAll.trim().split(), but that's not consistent
String[] tokens = WHITESPACE_PATTERN.split(readAll());
if (tokens.length == 0 || tokens[0].length() > 0)
return tokens;
String[] op = new String[tokens.length-1];
for (int i=0; i < tokens.length-1; i++)
op[i] = tokens[i+1];
savePosition();
return op;
}
public static String[] readStrings()
{
return readAllStrings(); //call to readAllStrings
}
TRY THE ABOVE CODE !!! It runs....