You are not logged in. Please login at www.codechef.com to post your questions!

×

Problem in understanding the identifiers in Java

I am facing a serious problem now a days.

import java.io. * ;
import java.util. * ;
import java.text. * ;
import java.math. * ;
import java.util.regex. * ;
public class Solution {
    public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int d,q,x,y;
    ArrayList[] set = new ArrayList[n];
    for(int i=0;i < n;i++){
        d = in.nextInt();
        set[i] = new ArrayList();
        for(int j=0;j < d;j++){
set[i].add(in.nextInt());
} } q=in.nextInt(); for(int i=0;i < q;i++){ x=in.nextInt(); y=in.nextInt(); try{ System.out.println(set[x-1].get(y-1)); } catch(Exception e){ System.out.println("ERROR!"); } } } }

Above solution is correct and now I am giving the same solution with the different variable name of ArrayList and different Scanner object name I am getting a runtime error.

import java.io. * ;
import java.util. * ;
import java.text. * ;
import java.math. * ;
import java.util.regex. * ;
public class Solution {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int d,x,y,q;
    ArrayList[] a = new ArrayList[n];
    for(int i=0;i <  n;i++){
        d = sc.nextInt();
        a[i] = new ArrayList();
        for(int j=0;j < d;j++){  
            a[i].add(sc.nextInt());                
        }
    }
    q=sc.nextInt();
    for(int i=0;i < q;i++){
        x=sc.nextInt();
        y=sc.nextInt();
        try{
            System.out.println(a[x-1].get(y-1));
        } catch(Exception e){
            System.out.println("ERROR!");
        }
    }
}
} 

and this Solution sometime gives Runtime Error and some time it gives correct output. I really need to know the reason behind this issue.

asked 18 Aug '18, 13:44

apamit11's gravatar image

1★apamit11
775
accept rate: 16%

edited 18 Aug '18, 14:00

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,278
×530
×123
×102
×23

question asked: 18 Aug '18, 13:44

question was seen: 104 times

last updated: 18 Aug '18, 14:00