简体   繁体   中英

Intellij Idea Auto Import not functioning

As the title suggests, Auto Import does not seem to work even though I have applied the correct settings.

在此输入图像描述

The posts I've looked up said to uncheck "Optimize imports on the fly" just in case I was automatically adding the imports but then Intellij realized the import was unnecessary so it removed it through optimizing. But as you can see, mine is not on to begin it.

I've also made sure to check mark the Auto Import box from File -> Other Settings -> Default Settings...

I am testing it with a simple program,

在此输入图像描述

I've done these same steps in Android Studio before and everything worked great so I'm at a loss why it does not want to work anymore. In Android Studio, as you typed in a new Object that was unambiguous, it would simply include the imports above. Any help would be much appreciated, thank you!!

The 'not working' link shows that IntelliJ cannot auto import because ...

  • The import is not unambiguous; the IDE reports:

     java.util.Scanner? (multiple choices ...) 
  • In your preferences you have ticked the option: Add unambiguous imports on the fly

So, for this import Scanner the IDE seems to be behaving itself correctly. To select a Scanner import just hit ALT ENTER , a popup will appear and you can choose from that.

Re this:

I've done these same steps in Android Studio before and everything worked great so I'm at a loss why it does not want to work anymore. In Android Studio, as you typed in a new Object that was unambiguous

That seem to summarise the issue quite well. If the import is unambiguous (ie if there is only one possible value for the import) then IntelliJ will auto create the import statement for you but if there are multiple possible values (as is the case for Scanner ) then you have to tell IntelliJ which one you want. Seems to me that IntelliJ is behaving correctly (and consistently with your chosen preferences) for the Scanner import.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM