I am solving this problem on ArrayList
in HackerRank. I completed the code but there's no stdout
showing on the console.
Question: You are given lines. In each line, there are zero or more integers. You need to answer a few queries where you need to tell the number located in the Yth position of Xth line.
The first line has an integer (n). In each of the next (n) lines there will be an integer denoting the number (d) of integers on that line and then there will be (d) space-separated integers. In the next line, there will be an integer denoting q number of queries. Each query will consist of two integers (x)and (y).
I am getting a number format exception at (int)number
.
Problem statement - if the number is present at x row and y position, print it otherwise print ERROR!
Input:
5
5 41 77 74 22 44
1 12
4 37 34 36 52
0
3 20 22 33
5
1 3
3 4
3 1
4 3
5 5
Output:
74
52
37
ERROR!
ERROR!
My code:
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) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
int n = 0;
String []problem;
int queries = 0;
int number = 0;
ArrayList<List<String>> allProblems = new ArrayList<>();
List<String> problems = new ArrayList<>();
ArrayList<List<String>> allPos = new ArrayList<>();
List<String> pos = new ArrayList<>();
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
sc.nextLine();
for(int i=0;i<n;i++)
{
problem = sc.nextLine().split(" ");
problems = Arrays.asList(problem);
allProblems.add(problems);
}
queries = sc.nextInt();
sc.nextLine();
for(int i=0;i<queries;i++)
{
pos = Arrays.asList(sc.nextLine().split(" "));
allPos.add(pos);
}
for(int i=0;i<queries;i++)
{
int x = Integer.parseInt((allPos.get(i)).get(0));
int y = Integer.parseInt((allPos.get(i)).get(1));
if(y >= allProblems.get(i).size()-1)
{
System.out.println("ERROR!");
}
else
{
number = Integer.parseInt((allProblems.get(x)).get(y+1));
System.out.println(number);
}
}
sc.close();
}
}