我想要问用户他们的文件的名称,然后我要扫描该文件,以查看该文件中有多少索引,然后将其放入一个数组,然后从那里进行。如何向用户询问输入文件名,然后使用该文件名?
这里是我的代码:
import java.util.*;
import java.io.*;
public class TestScoresAndSummaryStatistics {
public static void main(String[] args) throws IOException {
int scores;
int indices = -1;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name of the file");
String fileName = keyboard.next();
//I believe something is wrong here, am I incorrectly bring inputFile into new File?
File inputFile = new File(fileName);
Scanner data = new Scanner(inputFile);
while (data.hasNext()) {
indices++;
}
System.out.println("There are: " + indices + "indices.");
}
}
我相信出事了与= new File(filename);
部分:也许是因为我没有报价,但我不能完全肯定。我怎样才能解决这个问题?
你遇到了什么错误? – APerson 2014-10-18 23:32:15
你的错误可能是'keyboard.next()'调用;尝试将其改为'keyboard.nextLine();'。 – Pokechu22 2014-10-18 23:33:07
我不明白C++部分,那是什么? – Lrrr 2014-10-18 23:33:35