Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Benjamin Young
5,881 PointsError in Java: Cannot find symbol
This is probably an easy fix but I guess I have looked at it too long to figure it out. My error is:
javac Example.java && java Example
Example.java:16: error: cannot find symbol
for (String hashtag : originalTreet.getHashTags()) {
^
symbol: method getHashTags()
location: variable originalTreet of type Treet
Example.java
import java.util.Arrays;
import java.util.Date;
import com.teamtreehouse.Treet;
import com.teamtreehouse.Treets;
public class Example {
public static void main(String[] args) {
Treet[] treets = Treets.load();
System.out.printf("There are %d treets. %n",
treets.length);
Treet originalTreet = treets[0];
System.out.println("Hashtags:");
for (String hashtag : originalTreet.getHashTags()) {
System.out.println(hashtag);
}
}
}
And in Treet.java
package com.teamtreehouse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class Treet implements Comparable<Treet>, Serializable {
private static final long serialVersionUID = 7146681148113043748L;
private String mAuthor;
private String mDescription;
private Date mCreationDate;
public Treet(String author, String description, Date creationDate) {
mAuthor = author;
mDescription = description;
mCreationDate = creationDate;
}
@Override
public String toString() {
return String.format("Treet: \"%s\" by %s on %s",
mDescription, mAuthor, mCreationDate);
}
@Override
public int compareTo(Treet other) {
if (equals(other)) {
return 0;
}
int dateCmp = mCreationDate.compareTo(other.mCreationDate);
if (dateCmp == 0) {
return mDescription.compareTo(other.mDescription);
}
return dateCmp;
}
public String getAuthor() {
return mAuthor;
}
public String getDescription() {
return mDescription;
}
public Date getCreationDate() {
return mCreationDate;
}
public List<String> getWords() {
String[] words = mDescription.toLowerCase().split("[^\\w#@']+");
return Arrays.asList(words);
}
public List<String> getHashtag() {
return getWordsPrefixedWith("#");
}
public List<String> getMentions() {
return getWordsPrefixedWith("@");
}
private List<String> getWordsPrefixedWith(String prefix) {
List<String> results = new ArrayList<String>();
for (String word : getWords()) {
if (word.startsWith(prefix)) {
results.add(word);
}
}
return results;
}
2 Answers
Jeeya M
16,839 PointsYou said getHashtags < with an s on the end. The method is getHashtag()
andren
28,558 Points"cannot find symbol" usually means Java cannot find the method, which usually means you made a typo either when calling the method or when defining the method.
Looking quickly over the code I see that Treet has a method called getHashtag(), you are calling getHastags() that is what is causing the error.
Benjamin Young
5,881 PointsBenjamin Young
5,881 PointsThe same error popped up, I have done several iterations of that fix and it returns the exact same error. If it were that simple I would not be stuck on this problem, thank you for helping though. As a reference this is in Java Data Structures through Craig Dennis, specifically "Using Array Lists" where everything seems to be syntactically the same as his code as per the course video. For redundancies' sake yes I have saved the new code in workspace before running it again.