所以,我做这个节目是为了学习java练习后,javac的符号没有发现错误,进口的java.nio
import java.util.Scanner;
import java.io;
import java.nio;
class wordsInLine {
public static void main(String args[]) {
int wordCount;
//checking for correct syntax
if (args.length > 1){
System.out.println("Usage : java wordsInLine [path to text file to scan for words, ommit if you want to be asked for input]");
System.exit(0);
}
//if the user has provided a path to a text file then read the file
if (args.length == 1) {
Path file = args[0];
byte[] fileBytes;
fileBytes = Files.readAllBytes(file);
//convert the text of the file to a big string
String line = new String(fileBytes, StandardCharsets.UTF_8);
}else{ // else the user wants to give us his/her own line
//getting the users's input
Scanner input = new Scanner(System.in);
String line = input.nextLine();
}
//call the word count function
wordCount = countWords(line);
//print result
System.out.println(wordCount + " words found in the input line");
}
public static int countWords(String line){
//vars for easier understanding and reading
final boolean IN = true;
final boolean OUT = false;
boolean isLetter;
boolean STATE = false;
//just counting the words here
int wordCount = 0;
//looping through every character of the user's input line
for (int i = 0, n = line.length(); i < n; i++){
isLetter = Character.isLetter(line.charAt(i));
if (isLetter == true){
STATE = IN;
}
if ((STATE == IN) && (isLetter == false)){
wordCount++;
STATE = OUT;
}
if (((STATE == IN) && (line.charAt(i) == '\n')) || ((STATE == IN) && (i == (line.length() - 1)))){
wordCount++;
STATE = OUT;
}
}
return wordCount;
}
}
我尝试编译此使用javac wordsInLine.java
我也得到 wordsInLine.java:2: error: cannot find symbol import java.io; ^ symbol: class io location: package java wordsInLine.java:3: error: cannot find symbol import java.nio; ^ symbol: class nio location: package java wordsInLine.java:17: error: cannot find symbol Path file = args[0]; ^ symbol: class Path location: class wordsInLine wordsInLine.java:19: error: cannot find symbol fileBytes = Files.readAllBytes(file); ^ symbol: variable Files location: class wordsInLine wordsInLine.java:20: error: cannot find symbol String line = new String(fileBytes, StandardCharsets.UTF_8); ^ symbol: variable StandardCharsets location: class wordsInLine 5 errors
第二和第三import语句我在网上找到,如果我略过,我尝试编译我得到
wordsInLine.java:15: error: cannot find symbol
Path file = args[0];
^
symbol: class Path
location: class wordsInLine
wordsInLine.java:17: error: cannot find symbol
fileBytes = Files.readAllBytes(file);
^
symbol: variable Files
location: class wordsInLine
wordsInLine.java:18: error: cannot find symbol
String line = new String(fileBytes, StandardCharsets.UTF_8);
^
symbol: variable StandardCharsets
location: class wordsInLine
我一派,有很多人说,我也许有一个过时的Java和/或javac的版本,
java -version
回报
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
也
javac -version
回报
javac 1.8.0_101
我刚开始学习java,我碰到了一堵砖墙, 有人能告诉我我在这里错过了什么吗?
编辑:改变第二和第三import语句(由@EJP建议)之后,我得到:
wordsInLine.java:17: error: cannot find symbol
Path file = args[0];
^
symbol: class Path
location: class wordsInLine
wordsInLine.java:19: error: cannot find symbol
fileBytes = Files.readAllBytes(file);
^
symbol: variable Files
location: class wordsInLine
wordsInLine.java:20: error: cannot find symbol
String line = new String(fileBytes, StandardCharsets.UTF_8);
^
symbol: variable StandardCharsets
location: class wordsInLine
3 errors
这些语句的
我试过了,它的工作原理,我没有在导入语句中找到符号未找到的错误,但是我在Path,Files和StandardCharsets.UTF_8上找到它们。查看我的问题的底部以获取更多详细信息。另外,如何导入'java.util.Scanner;'是有效的,但是'import java.io;'不是? – spirosbax
所以你需要做我在我答案的最后一行所说的话。 2016年你自己没有理由自己输入import语句。你最终的问题的答案是相当简单的Java,可以在JLS中找到。 – EJP