所以,我正在将这个程序作为练习来学习java,
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
我在网上找到的第二和第三个导入语句,如果我省略它们并且我尝试编译我得到
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而且我已经打了一堵砖墙, 谁能告诉我,我在这里失踪了什么?
编辑:更改第2和第3个导入语句(由@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
答案 0 :(得分:0)
import java.io;
import java.nio;
这些陈述都不是合法的Java。你需要
import java.io.*;
import java.nio.*;
或更好仍然使用您的IDE功能为您执行导入。