Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(JAVA-W0146) Throw exception in next method #10

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/edu/duke/TextIterable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.duke;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* This utility class allows multiple classes to iterate over a text source in multiple ways.
Expand All @@ -12,7 +13,7 @@
* http://www.apache.org/licenses/LICENSE-2.0 for details.
*/
class TextIterable implements Iterable<String> {
private String[] myStrings;
private final String[] myStrings;

/** Create from a given string. */
public TextIterable(String source, String regexp) {
Expand All @@ -33,7 +34,11 @@ public boolean hasNext() {
}

@Override
public String next() {
public String next() throws NoSuchElementException {
if (!hasNext()) {
throw new NoSuchElementException("No more elements in the iteration");
}

String s = myStrings[myCount];
myCount++;
return s;
Expand Down