错误不在while条件
我加载了一个文件(该文件包含由tab分隔的三列数字),我收到此错误:
java.util.NoSuchElementExceptionat java.util.Scanner.throwFor(Unknown Source)at java.util.Scanner.next(Unknown Source)at webscience.Main.countTimesListened(Main.java:43)at webscience.Main.main (Main.java:21)
System.out.println ("Program Starts");
File timesListened = Loader.loadFile();
countTimesListened(timesListened);
}
public static void countTimesListened (File file) throws FileNotFoundException
{
Scanner sc;
UserList userList = new UserList();
TimesListened timesListened = new TimesListened();
try
{
sc = new Scanner (file);
sc.useDelimiter("\\t");
while (sc.hasNextLine() && sc.nextLine() != "")
{
sc.reset();
String userID = sc.next();
String artistID = sc.next();
String weight = sc.next();
userID = userID.replaceAll("\\t", "");
artistID = artistID.replaceAll("\\t", "");
weight = weight.replaceAll("\\t", "");
User user = new User (userID);
Artist artist = new Artist (artistID);
user.listensTo(artist, weight);
if (!userList.userExists(user))
userList.addUser(user);
int num = Integer.parseInt(weight);
if (!timesListened.artistExists(artistID))
timesListened.addArtistWeight(artistID, num);
else
timesListened.increaseArtistWeight(artistID, num);
System.out.print(userID + artistID + weight);
}
sc.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (NoSuchElementException e)
{
e.printStackTrace();
}
File outputFile = new File ("user_artists.txt");
FileOutputStream fos = new FileOutputStream(outputFile);
PrintStream ps = new PrintStream(fos);
System.setOut(ps);
System.out.println(timesListened.toString());