ejaksla
ejaksla

Reputation: 128

Java String Parser to find string, save it and print it

I wrote this little part down here to find tags in email subjects, extract them and save them to my Set.

I.e. if email subject is "Hello there #bro #senpai" it should find and extract both 'bro' and 'senpai' and put it into m_aTags (which is just custom type Set - this.m_aTags = new HashSet(); - that's how i create it)

in m_aTags i put all the new tags as an MyTag objects.

public ADOTag(String sTagName){
  this.sTagName = sTagName;
}

Here is pretty straightforward constructor.

private void parseSubject(String sSubject) {

        Pattern aPattern = Pattern.compile("#(\\w+|\\W+)");
//this patter checks for '#anyword'
        Matcher aMatcher = aPattern.matcher(sSubject);

        this.m_sSubject = sSubject;

        while (aMatcher.find()) {
            String sTagPart = aMatcher.group();
            this.m_aTags.add(new MyTag(sTagPart));
                }
        for (MyTag s : m_aTags) {
            System.out.println(s);
        }

    }

I try to print it from Set in this for loop, but instead of Strings i get something like : com.xxx_ee.e_mail.MyTag@72433225

com.xxx_ee.e_mail.MyTag@12312325

I'm not sure why is that showing instead of just "bro" and "senpai". I want to print it and use it as string, I can't think of anything what will do, as I tried few things already.

I'm not sure what to do, anyone can help me?:)

ANSWER: I had to override toString() method in MyTag class :)

@Override
public String toString() {
    return String.format(sTagName);
}

Upvotes: 1

Views: 94

Answers (4)

a problem solver
a problem solver

Reputation: 316

System.out.println(s) calls System.out.println(Object object), this method just prints object.toString(). But when you call toString() to s, you are actually calling Object.toString(). And this is how the Object.toString() looks like:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

To change this result, overide toString() in your class MyTag.

Upvotes: 0

Ori Lentz
Ori Lentz

Reputation: 3688

When you do System.out.println(s); you're basically calling s.toString(). However, since s is of type MyTag and MyTag doesn't implement toString() on its' own, it calls the super.

Each object in Java is derived from Object. Object's toString() prints this "gibberish" which is basically the name of the object and the address it is located on.

If you want the object to do some other kind of printing (like the tag value), you need to override the method yourself. For example:

public MyTag(String sTagName){
  this.sTagName = sTagName;
}

@Override
public String toString() {
   return this.sTagName;
}

Upvotes: 1

dosw
dosw

Reputation: 431

It looks like your class MyTag is missing a good toString() implementation like

@Override
public String toString() {
    return sTagName;
}

Upvotes: 0

GregH
GregH

Reputation: 5459

You are printing the object. Try overriding the toString method in your MyTag class and call toString on Object s when you print your object

or if you have a method in the MyTag class to get the tagName then use it. for Example something like

System.out.println(s.getTag());

Upvotes: 1

Related Questions