我正在尝试(只是为了使分级电影更加容易;将您要添加的电影与列表中的其他电影进行比较,以了解适合的电影很难并且效率不高)该程序的工作方式类似于binary insertion sort
,用于以正确的顺序将电影添加到其他电影的列表中。在确定电影应该播放的列表中的位置的部分中,我遇到了错误
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Rater.binarySearch(Rater.java:32)
at Rater.main(Rater.java:24)
我不确定是什么原因造成的。代码如下:
import java.awt.List;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Rater
{
public static void main(String[] args)throws Exception
{
File file = new File("E:\\topmovies.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
ArrayList<String> movList = new ArrayList<String>();
while ((st = br.readLine()) != null) {
movList.add(st);
}
br.close();
while(true) {
System.out.println("Enter a movie to add.");
Scanner myObj = new Scanner(System.in);
String userName = myObj.nextLine();
myObj.close();
int index = binarySearch(movList, userName, 0, movList.size()-1);
movList.add(index, userName);
}
}
static int binarySearch(ArrayList<String> a, String item, int low, int high)
{
Scanner myObj3 = new Scanner(System.in);
if (high <= low) {
System.out.println("Did you like " + item + " more than " + a.get(low) + "?");
String answer2 = myObj3.nextLine().toLowerCase();
myObj3.close();
if(answer2.equals("yes")) {
return low + 1;
}
return low;
}
int mid = (low + high)/2;
if(item == a.get(mid)) {
myObj3.close();
return mid+1;
}
System.out.println("Did you like " + item + " more than " + a.get(mid) + "?");
String answer2 = "";
if(myObj3.hasNextLine()){
answer2 = myObj3.nextLine().toLowerCase();
}
myObj3.close();
if(answer2.equals("yes")) {
return binarySearch(a, item, mid+1, high);
}
return binarySearch(a, item, low, mid-1);
}
}
请忽略不良的命名约定,以及该名称可能尚未达到预期功能的事实。现在,我只是想修复此扫描仪不起作用。
答案 0 :(得分:1)
我不确定您的最终目标是什么,但是我可以通过以下方法解决您的错误:
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Rater
{
static Scanner myObj = new Scanner(System.in);
public static void main(String[] args)throws Exception
{
File file = new File("C:\\Users\\hparekh\\OneDrive - Visa Inc\\Documents\\test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
ArrayList<String> movList = new ArrayList<String>();
while ((st = br.readLine()) != null) {
movList.add(st);
}
br.close();
while(true) {
System.out.println("Enter a movie to add.");
String userName = myObj.nextLine();
int index = binarySearch(movList, userName, 0, movList.size()-1);
movList.add(index, userName);
}
}
static int binarySearch(ArrayList<String> a, String item, int low, int high)
{
if (high <= low) {
System.out.println("Did you like " + item + " more than " + a.get(low) + "?");
String answer2 = myObj.nextLine().toLowerCase();
if(answer2.equals("yes")) {
return low + 1;
}
return low;
}
int mid = (low + high)/2;
if(item == a.get(mid)) {
return mid+1;
}
System.out.println("Did you like " + item + " more than " + a.get(mid) + "?");
String answer2 = "";
if(myObj.hasNextLine()){
answer2 = myObj.nextLine().toLowerCase();
}
if(answer2.equals("yes")) {
return binarySearch(a, item, mid+1, high);
}
return binarySearch(a, item, low, mid-1);
}
}
希望这会有所帮助。祝你好运。