0

What does this mean?

I am trying to print an array pf strings that is about 570,000 strings long...I believe that is what this relates to.

It prints out when I run my program. Ignore the commented out code; this program is a work in progress.

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.
}

}

Partha
  • 19
  • 1
  • 3

2 Answers2

0

You are printing a string array, not a string.

This is just the way Java prints arrays. To see each string, try

Arrays.toString(yourArray)

By the way, see this Stack Overflow answer to see the reason why you are seeing what you are seeing.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
0
java.lang.String;@50f6d9ca

What that means is that you are trying to print the memory location that was allocated from your words array which was printed here:

System.out.println(words);
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63