Reputation: 45
I've got to use the method split()
as in the following code, but the compiler throws an error:
String defA1[] = new str1.split(" ");
The error thrown says:
error: package str1 does not exist
String defA2[] = new str1.split(" ");
^
Obviously it is not a package, str1
is a string previously defined that comes as an argument of a method, as following:
public static int calculateLevenshteinDistance(String str1, String str2) { ... }
I am also importing the package java.lang
import java.lang.*;
and even explicitly
import java.lang.String;
Please do not suggest StringTokenizer
, I want to know why this is not working.
Also you could help me with something else. I tried importing the package as static just wondering if that could fix it:
import static java.lang.*;
But the compiler would say:
error: cannot find symbol
import static java.lang.*;
^
It only happens when I add "static" to the import and it happens with any static import I dont know why.
I am using the JVM to compile.
Upvotes: 1
Views: 104
Reputation: 85779
Your syntax is wrong. You should just use String defA1[] = str1.split(" ");
. The new
keyword is meant to call a class constructor and create a new instance of the class. More info: Oracle Java Tutorial: Creating Objects
By the way, Java compiler already adds import java.lang.*
by you, no need to add a static
import to this package.
One more note, the JVM (the JRE) is not meant to compile the code, just to run it. You compile Java code using the Java SDK. More info: What is the difference between JDK and JRE?
Additional: If you want to know why the compiler gives you the message error package str1 does not exist
The compiler is trying to read this line
new str1.split(" ");
as Create a new instance of the class split
that is inside the str1
package and send a String
parameter to the constructor with value " "
. This is obviously wrong.
Upvotes: 4
Reputation: 1765
use this
String defA1[] = str1.split(" ");
No need of new operator
Upvotes: 2