this is my code
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
class ChefAndNumbers
{
public static void main (String[] args) throws Exception
{
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for(int i=0; i<n; i++){
int size = scan.nextInt();
int[] arr = new int[size];
for(int j=0; j<size; j++){
arr[j] = scan.nextInt();
}
Set hSet = new HashSet<>();
for(int m=0; m<arr.length; m++)
hSet.add(arr[m]);
Iterator iter = hSet.iterator();
int max = 0;
int result = 0;
while(iter.hasNext()){
int temp = Integer.valueOf(iter.next());
int count = 0;
int temp_count = 0;
for(int x=0; x<arr.length; x++){
if(arr[x] == temp)
temp_count++;
else{
if(temp_count > 1){
if(temp_count%2 != 0){
count = count + (temp_count/2 + 1);
}
else
count = count + temp_count/2;
}
else if(temp_count == 1)
count++;
temp_count = 0;
}
}
if(temp_count != 0) {
if(temp_count%2 != 0){
count = count + (temp_count/2 + 1);
}
else
count = count + temp_count/2;
}
if(count > max){
result = temp;
max = count;
}
}
System.out.println(result);
}
scan.close();
}
}
Exception in thread “main” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at ChefAndNumbers.main(Main.java:11)
With same code on eclipse ide, I am not getting any exception.