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
Allen C
1,448 PointsWhat's wrong with this Java code? (Trying to find if a value is in a list)
For the madlibs part of the Java Basics course, I'm trying to make all the "Bad Words" that are not allowed into a list, and then see if the noun (string) variable is a word that is part of the list.
I just started with Java so a lot of these new functions and methods are still very confusing. :(
This is the part of the code that I'm having trouble with, after doing some researching on the internet. Most terms and code I came across I had no clue as to what they meant or how they were used so I tried settling with this:
String[] badWords = new String[]{
"dork",
"idiot",
"nerd",
"jerk",
"dumbdumb"
};
if (Arrays.asList(badWords).contains(noun)){
console.printf("That is not a nice word. Please try something else.");
The error is the ever-so-often "Cannot find symbol". Any help would be greatly appreciated!
1 Answer
Simon Coates
28,695 Pointstried demoing your code at https://repl.it/languages/java
import java.util.Arrays;
class Main {
public static void main(String[] args) {
//String noun ="barney the dinosaur";
String noun ="dork";
String[] badWords = new String[]{ "dork", "idiot", "nerd", "jerk", "dumbdumb" };
if (Arrays.asList(badWords).contains(noun)){
System.out.println("That is not a nice word. Please try something else.");
}
}
}
The code snippet you posted is okay, but may require the import statement.
Allen C
1,448 PointsAllen C
1,448 PointsThanks a lot, importing java.util.Arrays worked!
If you don't mind, I have another question:
How is it that a programmer knows which libraries to import from in order to execute all of their code? How would I know (when) to import the java.util.Arrays library or any other library before writing a statement that would require something from it?
Thanks!
Simon Coates
28,695 PointsSimon Coates
28,695 PointsThere are one or two packages that are imported by default. I'll often look at the documentation and see where the class comes from. However, when you use an IDE, it'll prompt you when you make reference to something that requires an import (and give you suggestions).
When working without an IDE, the cannot find symbol message means that Java has encountered something that it doesn't understand. Two obvious reasons are spelling mistake, or needing to import. After getting the message that it didn't understand what Arrays referred to, i looked up the documentation, noticed that this class fell in one of the packages that you need to import to use, and then imported.
Hope this helps.
Allen C
1,448 PointsAllen C
1,448 PointsIt definitely did. Thanks for all your help!