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

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");

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!