10

I am not iterating the LinkedList by any means like scanner or other methods, I am using Collections.max() to get maximum number from the LinkedList.

I have read on Stack Overflow that this exception is thrown due to iterator or scanner or tokenizer, but I am using none of them.

import java.io.*;
import java.util.*;

class TLG {
    public static void main(String[] args)throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        LinkedList<Integer> first = new LinkedList<Integer>();
        LinkedList<Integer> second = new LinkedList<Integer>();

        int cases = Integer.parseInt(br.readLine());

        for(int i=1;i<=cases;i++) {
            String score = br.readLine();
            int number1 = Integer.parseInt(score.split(" ")[0]); 
            int number2 = Integer.parseInt(score.split(" ")[1]); 
            int diff = number1 - number2;

            if(diff > 0){
                first.add(diff);    
            }
            else {
                second.add(java.lang.Math.abs(diff));    
            }
        }

        Integer max1 = Collections.max(first);  // Getting Exception here
        Integer max2 = Collections.max(second); // Getting Exception here

        if(max1 > max2) {
            System.out.println(1+" "+max1);
        }
        else {
            System.out.println(2+" "+max2);
        }
    }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
PankajKushwaha
  • 878
  • 2
  • 11
  • 25

3 Answers3

9

You must be calling Collections.max() on an empty List.

As the Javadoc states, this results in NoSuchElementException:

<T extends Object & Comparable<? super T>> T java.util.Collections.max(Collection<? extends T> coll)

...

Throws:

ClassCastException - if the collection contains elements that are not mutually comparable (for example, strings and integers).

NoSuchElementException - if the collection is empty.

Eran
  • 387,369
  • 54
  • 702
  • 768
7

you are calling Collections.max() with empty list.

Suresh Sajja
  • 552
  • 3
  • 8
1

You are not checking against Empty case: So Collections.max() will throw NoSuchElementException

First check if any of the Lists is Empty (then you know the max and which list is providing it)

    Integer max;
    int list;
    if (first.isEmpty()) {
        max = Collections.max(second);
        list = 2;
    } else if (second.isEmpty()) {
        max = Collections.max(first);
        list = 1;
    } else {
        Integer max1 = Collections.max(first);
        Integer max2 = Collections.max(second);
        if (max1 > max2) {
            max = max1;
            list = 1;
        } else {
            max = max2;
            list = 2;
        }
    }
    System.out.println(list + " " + max);
gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
  • You are only hiding the problem there.. The real question is why these lists are empty? – user2336315 Oct 22 '14 at 18:57
  • @user2336315 Not really, what if `int diff = number1 - number2;` is always positive? Then `diff` will always be on the first list – gtgaxiola Oct 22 '14 at 19:19