这是什么意思?
我正在尝试打印一个长度约为570,000个字符串的pf字符串......我相信这就是它所涉及的内容。
我运行程序时打印出来。忽略已注释掉的代码;这个计划正在进行中。
package nGram;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
public class HashMapFun {
public static void main(String[] agrs) throws FileNotFoundException {
HashMap <String, Integer> wordFrequency = new HashMap <String, Integer> ();
String bookContent = getFile("AtlasShrugged.txt");
//Remove punctuation marks from string "bookContent"
//Split the words using spaces
String[] words = bookContent.split(" ");
System.out.println(words);
// bookContent.replace(".", "");
// bookContent.replace("!", "");
// bookContent.replace("?", "");
// bookContent.replace("(", "");
// bookContent.replace(")", "");
// bookContent.replace("-", "");
// bookContent.replace(";", "");
//Go to every word in the list
for (String word : words) {
//If I have already added the word to the frequency map
if (wordFrequency.containsKey(word)) {
int freq = wordFrequency.get(word);
freq = freq + 1;
wordFrequency.put(word, freq );
}
else {
//If not, add to HashMap
wordFrequency.put(word, 1);
}
}
// Iterator iterator = wordFrequency.keySet().iterator();
//
// while (iterator.hasNext()) {
// String key = iterator.next().toString();
// String value = wordFrequency.get(key).toString();
//
// System.out.println(key + " " + value);
// PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
// System.setOut(out);
}
//}
public static String getFile(String path) {
// Make a File object to represent this file at the path
File f = new File(path);
// Do the code in the try, and if it fails do the catch code
try {
// Make a scanner to read the file
Scanner fileScanner = new Scanner(f);
// Make a StringBuilder to create the file content
StringBuilder content = new StringBuilder();
// While the file scanner still has a line of input
while (fileScanner.hasNextLine()) {
// Append the next line of file input
content.append(fileScanner.nextLine());
// Append a newline character.
content.append("\n");
}
// Return whatever is in the StringBuilder
return content.toString();
}
// Catch any error that may occur in the above try statement
catch (FileNotFoundException e) {
System.out.println("Didn't find the file.");
}
return ""; // If all else fails, return an empty string.
}
}
答案 0 :(得分:0)
您正在打印字符串 array ,而不是字符串。
这就是Java打印数组的方式。要查看每个字符串,请尝试
Arrays.toString(yourArray)
顺便说一句,请参阅this StackOverflow answer,看看为什么会看到你所看到的内容。
答案 1 :(得分:0)
java.lang.String;@50f6d9ca
这意味着您正在尝试打印从memory location
分配的words array
:
System.out.println(words);