0

So I am trying to organize and array of strings in alphabetical order and insert them into a binary tree. I first took in a text file and used scanner to read it into a string. I then stripped away all punctuation and made all of the letters lowercase. Finally I turned my string into and array of words and sorted them using Arrays.sort(). however when I run the program to see if I can print my list, the only output I get is:

[Ljava.lang.String;@4965391b

I'm not sure what this means or why I am getting it as an output please help.

my code is below.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Arrays;

import java.util.Scanner;


public class Tester {

public static void main(String[] args) throws FileNotFoundException {

    Tester run = new Tester();
    run.it();

}

public void it() throws FileNotFoundException { 

    BTree theTree = new BTree();

    String str = this.readInFile();

    str = stripPunctuation(str);

    String myWords [] = breakIntoWords(str);

    //theTree.print();

}

public String readInFile() throws FileNotFoundException {


    String myFile = "";
    int numWords = 0;

    Scanner myScan = new Scanner(new File("Dracula.txt"));

    while(myScan.hasNext() == true) {

        myFile += myScan.nextLine() + " ";

    }

    return myFile;

}

public String stripPunctuation(String myFile) {

    myFile.replace('.', ' ');
    myFile.replace(',', ' ');
    myFile.replace('!', ' ');
    myFile.replace('?', ' ');
    myFile.replace('(', ' ');
    myFile.replace(')', ' ');
    myFile.replace('"', ' ');
    myFile.replace('[', ' ');
    myFile.replace(']', ' ');
    myFile.toLowerCase();
    return myFile;

}

public String [] breakIntoWords(String myFile) {

    BTree thisTree = new BTree();

    String[] words = myFile.split("\\s+");

    Arrays.sort(words);

    System.out.print(words);

    return words;

}

}

public class BTNode {

private BTNode rightChild;
private BTNode leftChild;
private String myWord;
private int numWords;
private int numInstance;
private boolean uniqueWord;
private boolean isRoot;
private boolean isDeepest;

public BTNode(String myWord, int numWords){

    this.numInstance = 1;
    this.myWord = myWord;
    this.numWords = numWords;
    this.rightChild = null;
    this.leftChild = null;

}

public String getMyWord() {
    return myWord;
}

public void setMyWord(String myWord) {
    this.myWord = myWord;
}

public BTNode getRightChild() {
    return rightChild;
}

public void setRightChild(BTNode rightChild) {
    this.rightChild = rightChild;
}

public BTNode getLeftChild() {
    return leftChild;
}

public void setLeftChild(BTNode leftChild) {
    this.leftChild = leftChild;
}

public int getnumWords() {
    return numWords;
}

public void setnumWords(int numWords) {
    this.numWords = numWords;
}

public boolean isUniqueWord() {
    return uniqueWord;
}

public void setUniqueWord(boolean uniqueWord) {
    this.uniqueWord = uniqueWord;
}

public boolean isRoot() {
    return isRoot;
}

public void setRoot(boolean isRoot) {
    this.isRoot = isRoot;
}

public boolean isDeepest() {
    return isDeepest;
}

public void setDeepest(boolean isDeepest) {
    this.isDeepest = isDeepest;
}

public int getNumInstance() {
    return numInstance;
}

public void setNumInstance(int numInstance) {
    this.numInstance = numInstance;
}

}

public class BTree {

private BTNode root;
private int nodeCount;


public boolean add(String word, int numWords){

    BTNode myNode = new BTNode(word, numWords);

    if(root == null){

        root = myNode;
        nodeCount++;
        return true;

    }

    if(findNode(word)){

        int tmp = myNode.getNumInstance();
        tmp++;
        myNode.setNumInstance(tmp);
        return false;

    }

    BTNode temp = root;

    while(temp != null){

        if(word.compareTo(temp.getMyWord()) < 0) {

            if(temp.getRightChild() == null){

                temp.setLeftChild(myNode);
                nodeCount++;
                return true;

            } else {

                temp = temp.getRightChild();

            }

        } else {

                if(temp.getLeftChild() == null){

                    temp.setLeftChild(myNode);
                    nodeCount++;
                    return true;

                } else {

                    temp = temp.getLeftChild();

                }

        }

    }

    return false;

}

public boolean findNode(String word) {
    return mySearch(root, word);
}

private boolean mySearch(BTNode root, String word) {
    if (root == null) {
        return false;
    }

    if ((root.getMyWord().compareTo(word) < 0)) {
        return true;
    } else {
        if (word.compareTo(root.getMyWord()) > 0) {
            return mySearch(root.getLeftChild(), word);
        } else {
            return mySearch(root.getRightChild(), word);
        }
    }
}

public void print() {
    printTree(root);
}

private void printTree(BTNode root) {
    if (root == null) {
        System.out.print(".");
        return;
    }

    printTree(root.getLeftChild());
    System.out.print(root.getMyWord());
    printTree(root.getRightChild());

}

public int wordCount() {

    return nodeCount;

}

}
  • 5
    You're just seeing the output of calling `toString()` on an array. It's got nothing to do with sorting. Try calling `Arrays.toString(words)` instead. – Jon Skeet Dec 09 '14 at 21:22
  • 1
    possible duplicate of [What's the simplest way to print an array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-an-array) – The Guy with The Hat Dec 09 '14 at 21:23
  • Also Strings are immutable. So your strip ponctuation method does nothing at the moment. – Alexis C. Dec 09 '14 at 21:36

1 Answers1

0

You are printing an Array of Strings in System.out.print, so basically [Ljava.lang.String;@4965391b is the string representation of the reference to this array.

If you replace the part

System.out.print(words);

with

for (String word : words) {
    System.out.println(" " + word);
}

you will get the elements inside the Array.

Rayden78
  • 43
  • 1
  • 1
  • 9