0

I am having a code which inputs:

vgxgp
3
2 4
2 5
7 14 

Expected output: Yes No Yes

1st input line contains a string S, the name of the item she is demanding.

2nd line contains an integer Q, the number of pairs of integers that used to say “Yes” or “No” to her.

These pairs are given in order, following Q line, each contains 2 integers, a and b. (1-based indexing) I am getting error at if(line[a]==line[b]) as ArrayIndex out of bound.

import java.io.BufferedReader;
import java.io.InputStreamReader;

class Test {
    public static void main(String args[] ) throws Exception {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int t =Integer.parseInt(br.readLine());

        int[][] ar = new int[t][2];
        int k=0;

        for (int j = 0; j < t; j++) {       
            String[] input1=br.readLine().split(" ");
            for (int i = 0; i < 2; i++) {
                ar[k][i]=Integer.parseInt(input1[i]);     
            }
            k=k+1;
        }
        k=0;
        for (int i = 0; i < t-1; i++) {
            System.out.println(line[i]);    
            System.out.println(ar[k][0]);
            System.out.println(ar[k][1]);
            int a=ar[k][0];
            int b=ar[k][1];         
            System.out.println("hello"+line[i]);

            if(line[a]==line[b]) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
                k = k + 1;
            }
        }
    }
}
Diego C Nascimento
  • 2,801
  • 1
  • 17
  • 23
Mohit Darmwal
  • 275
  • 1
  • 5
  • 21
  • 1
    Please take a bit of time to edit the code you've posted - the indentation is all over the place, making it *really* hard to read. Look at the preview before you post, and ask whether that's what you'd want to read if you were trying to answer the question. – Jon Skeet Jan 16 '15 at 10:17
  • 2
    Try to debug it you will find the issue . – Mahesh Jan 16 '15 at 10:18
  • Also, your variable `k` is never really necessary and can be replaced with the variable of the outer for loops. – Niels Billen Jan 16 '15 at 10:21
  • @Mohit Darmwal Did one of the answers help you to solve your issue? – MWiesner Apr 28 '15 at 12:24
  • @MWiesner yes.. all the folks did helped me on my issue..thanks all – Mohit Darmwal Jun 08 '15 at 06:02

2 Answers2

2

As indicated by Niels Billen's answer your code uses wrong index positions for the array line as the length of this array is exactly 1 and thereby the index position to be used 0. Using an index position of > 0 (given a = 2 and b = 4) produces an ArrayIndexOutOfBoundsException.

I have simplified your code in terms of readability, but due to a lack of a clear problem description I have just added a "good guess" which produces your desired output (Yes No Yes).

public class Test {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] line = br.readLine().split(" ");
        int numberOfDataPairs = Integer.parseInt(br.readLine());

        int[][] dataPairs = new int[numberOfDataPairs][2];

        // read in the data pairs here
        for (int j = 0; j < numberOfDataPairs; j++) {

            String[] input1 = br.readLine().split(" ");
            for (int i = 0; i < 2; i++) {
                dataPairs[j][i] = Integer.parseInt(input1[i]);

            }
        }
        System.out.println(line[0]);
        // compare the data now
        for (int i = 0; i < numberOfDataPairs; i++) {

            int a = dataPairs[i][0];
            int b = dataPairs[i][1];

            // produces your desired output... via a modulo (%) operator
            // but it's a guess - as description of the problem is unclear!
            if ((b % a) == 0) {
                System.out.println("Yes");

            } else
                System.out.println("No");
        }
    }
}
MWiesner
  • 8,868
  • 11
  • 36
  • 70
  • got my mistake..using variable k in second loop is extra headache... but hlp me on ((b % a) == 0) how this comparing ?? – Mohit Darmwal Jan 16 '15 at 12:48
  • see here: http://www.cs.umd.edu/~clin/MoreJava/Intro/expr-mod.html - Again: most obviously this is not what you exactly want, I guess. Be more specific what the aim of your "comparison" is? In which case is the output YES and in which is it NO!? – MWiesner Jan 16 '15 at 13:18
  • @MWiesner...thanks man for the help..i want like if input string is coconut then if test cases are 2 then 2 inputs are like 2 3 & 4 5 .....then it should be comaparing string value at 2 & 3 i.e. o ad c if this is same the print YES otherwise O – Mohit Darmwal Jan 17 '15 at 13:45
1

Your array line only has one element vgxgp.. That's why you get the error. In the final loop you are trying to access line[a]==line[b] where a = 2, b=4. These elements do not exist and will throw the ArrayIndexOutOfBoundsException

Niels Billen
  • 2,189
  • 11
  • 12