Getting NZEC in the following Code

import java.io.IOException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Scanner;

class Lead_Game{
    private String Player1;
    private String Player2;
    private String leader;
    private int lead;
    
    public void setPlayer1(String score){
        Player1=score;
    }
    public void setPlayer2(String score){
        Player2=score;
    }
    public void setLeader(String leader){
        this.leader=leader;
    }
    public void setlead(int lead){
        this.lead=lead;
    }
    
    //Getters
    public String getPlayer1(){
        return Player1;
    }
    public String getPlayer2(){
        return Player2;
    }
    public String getLeader(){
        return leader;
    }
    public int getLead(){
        return lead;
    }
    public static void main(String[] args) throws IOException,NumberFormatException,NoSuchElementException,IllegalStateException{
        int n,t;
        Scanner sc=new Scanner(System.in).useDelimiter(" |\\n");
        t=Integer.parseInt(sc.next());
        for(int k=0;k<t;k++){
        n=Integer.parseInt(sc.next());
        ArrayList<Lead_Game> round=new ArrayList<Lead_Game>();
        for(int i=0;i<n;i++){
            Lead_Game r=new Lead_Game();
            r.setPlayer1(sc.next());
            r.setPlayer2(sc.next());
            round.add(r);
        }
        
       for(int i=0;i<n;i++){
           int player1=Integer.parseInt(round.get(i).getPlayer1());
           int player2=Integer.parseInt(round.get(i).getPlayer2());
           if(player1>player2){
               round.get(i).setlead(player1-player2);
               round.get(i).setLeader("Player1");
           }else{
                  round.get(i).setlead(player2-player1);
               round.get(i).setLeader("Player2");
           }
       }
       String player=round.get(0).getLeader();
       int max=round.get(0).getLead();
       for(int i=1;i<n;i++){
           if(max<round.get(i).getLead()){
               max=round.get(i).getLead();
               player=round.get(i).getLeader();
           }
       }
       if(player.equals("Player1")){
           System.out.println("1 "+max);
       }
       else{
           System.out.println("2 "+max);
       }
        }
    }
}

Can anyone tell me why I’m getting NZEC error in it??

You should at least give link to problem statement so that it easier to answer. Nobody is going to read your code without any hint of the question.

1 Like

See your class name!
public class in Java needs to be named as Main !

NZEC occurs when a program doesn’t have a return statement(for c users) or your program throws an exception!

import java.util.ArrayList;
import java.util.Scanner;

public class Main{
private Integer Player1;
private Integer Player2;
private String leader;
private int lead;

public void setPlayer1(Integer score){
    Player1=score;
}
public void setPlayer2(Integer score){
    Player2=score;
}
public void setLeader(String leader){
    this.leader=leader;
}
public void setlead(int lead){
    this.lead=lead;
}

//Getters
public Integer getPlayer1(){
    return Player1;
}
public Integer getPlayer2(){
    return Player2;
}
public String getLeader(){
    return leader;
}
public int getLead(){
    return lead;
}
public static void main(String[] args)
{
    int n;
    Scanner sc=new Scanner(System.in);
    	n=sc.nextInt();
    	ArrayList<Main> round=new ArrayList<Main>();
    	for(int i=0;i<n;i++)
    	{
    		Main r=new Main();
    		r.setPlayer1(sc.nextInt());
    		r.setPlayer2(sc.nextInt());
    		round.add(r);
    	}
   for(int i=0;i<n;i++){
       int player1=round.get(i).getPlayer1();
       int player2=round.get(i).getPlayer2();
       if(player1>player2){
           round.get(i).setlead(player1-player2);
           round.get(i).setLeader("Player1");
       }else{
              round.get(i).setlead(player2-player1);
           round.get(i).setLeader("Player2");
       }
   }
   String player=round.get(0).getLeader();
   int max=round.get(0).getLead();
   for(int i=1;i<n;i++){
       if(max<round.get(i).getLead()){
           max=round.get(i).getLead();
           player=round.get(i).getLeader();
       }
   }
   if(player.equals("Player1")){
       System.out.println("1 "+max);
   }
   else{
       System.out.println("2 "+max);
   }
sc.close();    
}

}

There were several problems with your code, which I noticed only after reading the problem statement.
I edited your code! You were taking input t, whereas you don’t need t in this sum. Moreover your integer parsing was also wrong!

The code now gives wrong answer indicating a logical error!

1 Like

Its http://www.codechef.com/problems/TLG but i’ve solved this problem in C & C++, I’m getting NZEC error in JAVA even though i’ve thrown all the exceptions

That i have also seen in FAQs, but i’ve thrown all the exceptions still i’m getting NZEC