我有一个从文本文件创建Webgraph的程序。每次运行它时,都会收到相同的消息,提示“扫描仪已关闭”。
//constant final variables
public static final String PAGES_FILE = "pages.txt";
public static final String LINKS_FILE = "links.txt";
// webgraph object var
private WebGraph web;
// searchEngine constructor.
public SearchEngine() throws FileNotFoundException {
web = WebGraph.buildFromFiles(PAGES_FILE, LINKS_FILE);
}
扫描仪部分
try {
System.out.println("Loading WebGraph data...");
SearchEngine engine = new SearchEngine();
System.out.println("Success!");
Scanner scanner = new Scanner(System.in);
我在try块的紧随其后的是scan.close(),它包含所有可能的用户输入。我还附上了运行程序后应该发生的情况的图像。任何想法可能出什么问题吗?如果需要任何其他代码,我可以提供。Sample Program 这是我得到的My Run
的图片这是“ buildFromFiles”的代码
public static WebGraph buildFromFiles(String pagesFile, String linksFile)
throws IllegalArgumentException, FileNotFoundException {
WebGraph webGraph = new WebGraph();
File filePages = new File(pagesFile);
File fileLinks = new File(linksFile);
if (filePages.exists() && fileLinks.exists() && filePages.isFile() && fileLinks.isFile()) {
Scanner pageScanner = new Scanner(filePages);
while (pageScanner.hasNextLine()) {
String[] pageData = pageScanner.nextLine().split("\\s+");
String url = pageData[0];
ArrayList<String> keywords = new ArrayList<String>();
for (int i = 1; i < pageData.length; i++) {
keywords.add(pageData[i]);
}
webGraph.addPage(url, keywords);
}
pageScanner.close();
Scanner linkScanner = new Scanner(fileLinks);
while (linkScanner.hasNextLine()) {
String[] linkData = pageScanner.nextLine().split("\\s+");
webGraph.addLink(linkData[0], linkData[1]);
}
linkScanner.close();
} else {
throw new IllegalArgumentException();
}
return webGraph;
}