public Object next() { try { String result = internal.readLine(); if (result == null) { throw new NoSuchElementException(); } else { return result; } } catch (IOException e) {
// The original exception is included in the message to notify the // client that an IOException has occurred. throw new NoSuchElementException(e.toString()); } }
請注重:要使其余的代碼能使用修改過的方法,我們還必須:
導入 java.util.NoSuchElementException。 修正 hasNext,使其不再調用 next 來進行測試。最簡單的修正方法是只要直接調用 internal.readLine()。