我看到你不能在数组上使用字符串标记器,因为你不能转换String()
为String[]
. 一段时间后,我意识到如果 inputFromFile 方法逐行读取它,我可以逐行标记它。我只是不知道该怎么做才能返回它的标记化版本。
我假设line=in.ReadLine();
我应该把StringTokenizer token = new StringTokenizer(line,",")
..放在行中,但它似乎不起作用。
有什么帮助吗?(我必须标记逗号)。
public class Project1 {
private static int inputFromFile(String filename, String[] wordArray) {
TextFileInput in = new TextFileInput(filename);
int lengthFilled = 0;
String line = in.readLine();
while (lengthFilled < wordArray.length && line != null) {
wordArray[lengthFilled++] = line;
line = in.readLine();
}// while
if (line != null) {
System.out.println("File contains too many Strings.");
System.out.println("This program can process only "
+ wordArray.length + " Strings.");
System.exit(1);
} // if
in.close();
return lengthFilled;
} // method inputFromFile
public static void main(String[] args) {
String[] numArray = new String[100];
inputFromFile("input1.txt", numArray);
for (int i = 0; i < numArray.length; i++) {
if (numArray[i] == null) {
break;
}
System.out.println(numArray[i]);
}// for
for (int i=0;i<numArray.length;i++)
{
Integer.parseInt(numArray[i]);
}
}// main
}// project1