Rahul Singh
Rahul Singh

Reputation: 1

how to fix NoSuchElementException in my code

How can i remove NoSuchElementException in my code? i am applying hasNextLine also but it is not taking it then also.

    public static void main (String[] args) throws java.lang.Exception{

        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        Scanner sc1=new Scanner(System.in);
        String c[]=new String[t];
        String d[]=new String[t];
        int b[]=new int[t];
        for(int i=0;i<t;i++){
          b[i]=sc.nextInt();
          String a[]=sc1.nextLine().split(" ");
          c[i]=a[0];
          d[i]=a[1];
        }

        for(int j=0;j<t;j++){
          new X().substr(c[j],d[j]);
        }
    }

Upvotes: 0

Views: 64

Answers (1)

mrinal
mrinal

Reputation: 375

Do a sc.hasNextLine() check before scanning the next line to make sure the next line exists. You don't need scanner declaration twice.

Upvotes: 2

Related Questions