4

I am taking the University of Stanford algorithms course (via Coursera). This course uses IntelliJ as the programming environment as well as well as some libraries built for the course. In particular, it uses some StdOut and StdIn libraries to deal with standard output and input.

If I just write my code and try to compile it via the terminal, it will throw the eror

error: cannot find symbol; symbol: variable StdOut.

I figured out that the problem can be solved by adding import statements:

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

However, IntelliJ deletes these statements as soon as I add them. But, if I compile again, it compiles without errors, so it looks like IntelliJ is importing these libraries and then deleting the statements.

The problem is, if I close and reopen the project, and compile again, it gives me again the missing symbol error, which forces me to re-write the import statements.

My question is, I don't like the idea of re-writting these statements every time I close and open the project, I just want to IntelliJ to stop messing with my code and leave the statements in there. Since I'm just a beginner, I want to focus on learning about the algorithms, not on fighting against IntelliJ features, so I'm OK with completely disabling any kind of auto import and I will just handle all the imports manually.

I went to settings/Editor/General/Autoimport and basically disable everything, and yet IntelliJ still deletes my import statements.

Thanks you for your help!

2
  • Please provide a sample project to check. The reason may be - a different environment context is used when you build it from IDE (when there are no import statements needed) and form terminal.
    – Andrey
    Commented Jan 4, 2019 at 8:39
  • Thank you, I solved it. For the purpose of this course, I am supposed to download one of their base projects that are built to work with the courses libraries, and simply add new classes to code the exercises. I did this and everything works fine. What I was doing is to create a new project from scratch. There is probably a way to make a project from scratch and make it work with the course libraries, but I'm not going to worry about it for the moment.
    – Miguel GC
    Commented Jan 4, 2019 at 19:05

1 Answer 1

5

It isn't actually deleting your import-statements, just folding them. With other words, it just doesn't show the lines in which the import-statements are, mainly for readability. You can disable this behaviour in the following manner: Disable the following:

Settings -> Editor -> General -> Code Folding -> Imports

However, if intelliJ really deletes the lines, disable the following:

Settings -> Editor -> General -> Auto Import -> Optimize imports on the fly
5
  • Thank you! I though that as well at first. But it is not that, it is actually deleting them. I checked the settings and code folding is disabled. I don't think I would need to insert the lines again upon reopening the project if they were simply folded.
    – Miguel GC
    Commented Jan 3, 2019 at 21:37
  • Thanks, I already did that, and the program still deletes them. I already closed the program and restarted it. It is worse than I though, I have to add the lines every time I compile. If I add the lines, they are deleted, but the compilation finishes successfully. But if I compile again, I get the error again, si I'm forced to add the lines again. I will probably have to re-install IntelliJ from scratch if I cannot solve this.
    – Miguel GC
    Commented Jan 3, 2019 at 22:24
  • Very strange indeed. Notify us if even reinstalling it doesn't work.
    – DWuest
    Commented Jan 3, 2019 at 22:32
  • The problem still persists even after re-installation. The program does compile successfully from tools/recompile, even without adding the import statements. But the compilation fails when I use the terminal, which is what I want to do. Even with the terminal the compilation works after adding the import statements, but because the lines are removed immediately I have to re-add them every time I compile. My guess is that it is a bug in the pre-packaged programming environment of the course. But thanks for your help!
    – Miguel GC
    Commented Jan 4, 2019 at 3:30
  • Have you tried importing an unused package? It shouldn't delete it. Does that mean you didn't download IntelliJ from its actual website? If you didn't try installing it from their actual website. If you are thinking about switching editors, I recommend Atom or Sublime Text.
    – DWuest
    Commented Jan 4, 2019 at 4:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.