Home > front end >  how to fix NoSuchElementException on arraylist question
how to fix NoSuchElementException on arraylist question

Time:12-31

**
 * To be used by “getCourseName”, “getCourseId”, and “getStudentId” methods.  
 * 
 * Gets the String that follows the line that starts with the given String. 
 * Gets and returns the String on the very next line.
 * If the given String doesn't exist, should return null.
 * 
 * Example(s):
 * - If an ArrayList<String> lines contains: "hello" and "world", and we call 
 * getNextStringStartsWith("hello"), we'll get the String "world".
 * 
 * - If an ArrayList<String> lines contains: "Course" and "CIT590", and we call 
 * getNextStringStartsWith("Course"), we'll get the String "CIT590".
 * 
 * - If an ArrayList<String> lines contains: "hello" and "world", and we call 
 * getNextStringStartsWith("goodbye"), we'll get null.
 * 
 * @param str to look for in lines
 * @return String from the very next line

how to fix java.util.NoSuchElementException on my code, here is my code:

private ArrayList<String> lines = new ArrayList<String>();

public InfoProcessor(ArrayList<String> lines) {
    this.lines = lines;
}

String getNextStringStartsWith(String str) {
    
    // TODO Implement method
    
    Iterator<String> it = lines.iterator();

    if (!lines.contains(str)) return null;
        
    while (it.hasNext() && !it.next().equals(str)) {
        it.next();
    }

    return it.next();        
}

If an ArrayList lines contains: "hello" and "world", and we call getNextStringStartsWith("goodbye"), we'll get null. I tried to use !lines.contains(str), but it does not work, can anyone help me with this, thank you!

CodePudding user response:

I have tried implementing the method getNextStringStartsWith using contains method, it does not return any error. I have shown the implementation below.

private static String getNextStringStartsWith(String str) {
    if (lines.contains(str)) {
        int res = lines.indexOf(str);
        return lines.get(res 1);
    }
    
    return null; 
}

Also, i'm assuming if the last element ( say "world") is passed in the example, then output will be null only as it the last element of the list.

CodePudding user response:

If the matched String is the last element in your list, of course you get NoSuchElementException because the iterator already reached the end.

A simple fix would be change return it.next(); to return it.hasNext() ? it.next() : null; But there are other "problems" in your code.

  1. Normally, you declare variables using the interface(List), instead of a specific implementation(ArrayList).
  2. the Big O of contains in List is O(n), you actually looped the list twice.
  3. no null handling

Below is my version of code

private List<String> lines = new ArrayList<>();

public InfoProcessor(List<String> lines) {
    this.lines = lines;
}

String getNextStringStartsWith(String str) {
    for (int i = 0; i < lines.size(); i  ) {
        String current = lines.get(i);
        boolean matched = false;

        if (Objects.isNull(str)) {
            if (Objects.isNull(current)) {
                matched = true;
            }
        } else if (str.equals(lines.get(i))) {
            matched = true;
        }

        if (matched && i < lines.size() - 1) {
            return lines.get(i   1);
        }
    }

    return null;
}
  •  Tags:  
  • java
  • Related