Getting recursively all components on JFrame [duplicate] - java

I get the following error despite it's exactly one used in examples:
error: type List does not take parameters
List<String> strings_wat = new ArrayList<String>();
Java is version 1.7 and the class is NOT named ArrayList.

You are likely importing java.awt.List.
You should instead import java.util.List, which is a parameterized type.

It seems like you are importing it from java.awt:
import java.awt.List;
and it looks like you want to use the one from java.util:
import java.util.List;

if you are working on Graphical user interface you have to
import java.awt.List instead of import java.util.List
Other then else if you are working on simple code you have to import java.util.List; instead of import java.awt.List

I am getting solved this problem by renaming the class name or file name as in Notepad++.
You Should not take the class names as ArrayList or List.
And you also need to add the following package
import java.util.*;
My Previous class name is List I changed it into the Names.so You have to change the class name.

Related

Import java.nio.file.Files and com.google.common.io.Files in 1 file is not allowed

I'm very new to java I need to use a different features from MAVEN dependencies but they have a same name like this,
import java.nio.file.Files;
import com.google.common.io.Files;
I do not allow me to import. I will throw error like
The import com.google.common.io.Files collides with another import statement
Can this be solved ? Thanks a lot.
When you have to use two classes with the same name, you have to import one and use the fully qualified name of the other one in the code.
For example, leave the first import. And when you want to create one variable of each type, you do the following:
import java.nio.file.Files;
public class MyClass{
Files files; //This variable uses the imported type
com.google.common.io.Files ioFiles; //This variable uses the explicit type
}

Facing a problem with using Natty library for converting string to date [duplicate]

I get the following error despite it's exactly one used in examples:
error: type List does not take parameters
List<String> strings_wat = new ArrayList<String>();
Java is version 1.7 and the class is NOT named ArrayList.
You are likely importing java.awt.List.
You should instead import java.util.List, which is a parameterized type.
It seems like you are importing it from java.awt:
import java.awt.List;
and it looks like you want to use the one from java.util:
import java.util.List;
if you are working on Graphical user interface you have to
import java.awt.List instead of import java.util.List
Other then else if you are working on simple code you have to import java.util.List; instead of import java.awt.List
I am getting solved this problem by renaming the class name or file name as in Notepad++.
You Should not take the class names as ArrayList or List.
And you also need to add the following package
import java.util.*;
My Previous class name is List I changed it into the Names.so You have to change the class name.

How do I import packages in Java?

I have a "Sprites" folder with some class files and a "Launcher" folder with some class files. I tried the following code for import:
package Sprites;
and it lead to the following
hw9\Launcher>javac *.java
TowerDefense.java:2: error: class, interface, or enum expected
package Sprites;
^
1 error
Am I doing this incorrectly? My Sprites and Launcher are in the hw9 directory, so I assumed it would work. A picture for clarification:
You can use a wildcard import to import all classes within the immediate directory:
import Sprites.*;
This opposed to something like:
import Sprites.Class1;
import Sprites.Class2;
import Sprites.Class3;
...
Generally, wildcard imports can produce conflicts and errors (for example java.awt.List and java.util.List), so usually better to avoid them.
Packages should also be lower-cased.
The error is due to syntax, usually when you see something like ...expected that is syntax error indicator.
In the class in your launcher package, include the import statements for the classes which are being referred to.
It should look something like the following:
package the.name.of.your.package;
import Spirites.NameOfclass; //quialify the import parth as is
class YourLauncherClass{
//class definition
}
Also make sure that semicolons aren't missing at the end of import and package.
Hope that helps.
Best practice is to import the specific class you require rather than importing the complete package.
import Spirites.NameOfclassRequired;
class YourClass{
//class definition
}
If you are using eclipse you can do that using CTRL+SHIFT+O When you do that eclipse imports the specific class you require. For an instance if you using an ArrayList rather than importing java.util.*; it will import java.util.ArrayList;
If you need multiple classes from a package then for sure you can import the entire package

error: type List does not take parameters

I get the following error despite it's exactly one used in examples:
error: type List does not take parameters
List<String> strings_wat = new ArrayList<String>();
Java is version 1.7 and the class is NOT named ArrayList.
You are likely importing java.awt.List.
You should instead import java.util.List, which is a parameterized type.
It seems like you are importing it from java.awt:
import java.awt.List;
and it looks like you want to use the one from java.util:
import java.util.List;
if you are working on Graphical user interface you have to
import java.awt.List instead of import java.util.List
Other then else if you are working on simple code you have to import java.util.List; instead of import java.awt.List
I am getting solved this problem by renaming the class name or file name as in Notepad++.
You Should not take the class names as ArrayList or List.
And you also need to add the following package
import java.util.*;
My Previous class name is List I changed it into the Names.so You have to change the class name.

Import a single class in Java

Simple question but even though googled it a lot I could not find the answer.
Is it possible to import a class outside a package?
Let's say I have 2 folders A and B with a .java file in each, is it possible by using the clause import to import the class contained in A? import A.Aclass ? or it's mandatory using package syntax whenever there is the keyword import?
Yes it is possible to import the class with the import statement. For better understanding let's assume that you have three folders foldera, folderb and folderc where foldera contains a .java file named "ClassA.java", folderb contains another .java file named "ClassB.java" and folderc contains a .java file named "ClassC.java". Now, if you want to uses the member data and operations of "ClassA.java" in "ClassC.java" you can use the import statement as shown below:
import foldera.ClassA
If you want to use the member data & operations of "ClassB.java" in "ClassC.java" it is also possible with the import statement
import folderb.ClassB
As per the java source file declaration rule, if the class is a part of a package, the package statement must be the first line in the source code file, before any import statements that may be present. In this example, the first line of "ClassC.java" source file must be package folderc since it is located in folderc. Similarly, the first line of "ClassA.java" source file must be package foldera, and the first line of "ClassB.java" source file must be package folderb.
Hope now you are clear with the concept!
Thank you...
Well, if the class is defined to have a package a; then you need to import the class with the package name. If you have two packages which contain a class with the same name, then in your class which needs to invoke each of them, you will need to use a fully-qualified name. For example:
import a.Foo;
import b.Foo;
public class Bar
{
public static void main(String[] args)
{
a.Foo aFoo = new a.Foo();
b.Foo bFoo = new b.Foo();
}
}
Alternatively, if you have two packages with classes of the same name, you can simply skip importing them, but rather -- using them by their fully-qualified names (FQN-s).
If the class does not have a package ...;, then simply import it as:
import Foo;
However, if you have two packages (from different libraries) which contain classes with identical FQN-s, then the first one on the classpath will be picked.
Please, bear in mind that the convention for naming packages is to use lowercase letters and for classes -- the name should start with an upper case letter for each word in the class' name.
Yes it is possible.
If you have the following:
Package: PackA
Class: ClasA
Do:
import PackA.ClassA; //Import the class
OR
import PackA.*; //Import all the classes within the package
yes it is possible just import the package
syntax
import pck.ClassA or import pck.*
Yes, you have to use package syntax.
importing all class inside folder A.
import com.pack.A.*;
importing specific class inside folder A.
import com.pack.ClassName;

Categories

Resources