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

Java Java Data Structures Exploring the Java Collection Framework Lists

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Jshell in InteliJ. Code ain't running

I have this in InteliJ:

import java.util.ArrayList;

List<String> fruit = new ArrayList<String>();

... and List and fruit are colored red so it looks like inteliJ didnt load the class ArrayList class. Why?

4 Answers

Collin Halliday
seal-mask
.a{fill-rule:evenodd;}techdegree
Collin Halliday
Full Stack JavaScript Techdegree Student 17,491 Points

To be perfectly honest, this is my first time ever using JShell through IntelliJ. I always use JShell through the terminal on my Mac. In fact, I did not even have JShell installed as a tool on my IntelliJ before I started trying to help you with this problem. Because of this, I might not be able to answer your question as well as someone that has more experience using the built-in console through IntelliJ.

As an aside, if you wish to attempt using JShell through the command line, you should be able to open a terminal window on your computer and type "jshell" to get it started. Through the command line tool, you should not need to import List or ArrayList, and typing the code "List<String> fruit = new ArrayList<>();" as you have above should not result in any errors.

After I saw the screenshot you sent me, I downloaded the JShell tool for IntelliJ and messed around with it for a little bit. I included your exact code and the program ran without any errors. The text entered into the JShell editor did have the red squiggly error lines in various places with error message popups when I hovered over the red lines. However, this did not affect the program's ability to run successfully. My sense is that the errors show up that way, because the IDE is set up for coding through classes inside of a project, and I think the error detection is tailored to that context.

Regardless, you can continue to run the program as many times as you would like, and the output from each time the program is run will append to the text inside the console window at the bottom of your screen. Looking at the screen shot you provided, it looks like you ran the program once without the List import, resulting in the error in your output, and again after you added the appropriate import, resulting in no error. Try clicking the red "X" on the console window that produces output from your program, then run the program again. You should not get any errors this time.

I hope that is somewhat helpful. Best of luck!

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

You are right, code works fine even though inteliJ indicates a problem. Thank you for you time, I appreciate your help.

Collin Halliday
seal-mask
.a{fill-rule:evenodd;}techdegree
Collin Halliday
Full Stack JavaScript Techdegree Student 17,491 Points

Hey, Andre.

I ran your code quickly in JShell and it worked for me. Did you include your import statement at the top of your java document outside of the class in which you are declaring and instantiating your ArrayList?

Andre Kucharzyk
Andre Kucharzyk
4,479 Points

Hello Collin :)

import java.util.ArrayList; import java.util.List;

List<String> fruit = new ArrayList<>();

When I run this code i get:

Defined import java.util.ArrayList

ERROR: cannot find symbol symbol: class List location: class Rejected field List<String> fruit

import java.util.ArrayList Defined import java.util.List

Defined field List<String> fruit = []

Collin Halliday
seal-mask
.a{fill-rule:evenodd;}techdegree
Collin Halliday
Full Stack JavaScript Techdegree Student 17,491 Points

I'm not sure exactly what the issue is. I ran the exact code you provided in my IntelliJ IDE and it worked just fine for me. Can you provide a screen shot of your code so I can see where everything is placed in your editor?