-1

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();
        }
    }
Robert
  • 7,394
  • 40
  • 45
  • 64
  • `I am getting a number format exception at (int)number.` -- Which line of code is causing that exception? – Robert Harvey Aug 26 '22 at 18:39
  • @RobertHarvey line causing the error at number = Integer.parseInt((allProblems.get(x)).get(y+1)); – Koushik Andhavarapu Aug 26 '22 at 18:43
  • That's a parsing failure. Whatever is in `(allProblems.get(x)).get(y+1)` is something that `parseInt()` doesn't recognize as an integer. There's a lot of moving parts in that line of code; consider breaking that line into several steps, so that you can see the results of each step. – Robert Harvey Aug 26 '22 at 20:04

1 Answers1

0

Please, try to use the code below. I added the comments in places where I changed something

package tests;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

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; // removed an implementation from here, it wasn't used
        ArrayList<List<String>> allPos = new ArrayList<>();
        List<String> pos; // removed an implementation from here, it wasn't used

        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);
        }
        sc.close(); // I closed the scanner as soon as I ended read data

        // defining the return values
        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(x-1).size() - 1) // the equal mark was not needed
            {
                System.out.println("ERROR!");
            } else
            {
                number = Integer.parseInt((allProblems.get(x-1)).get(y)); // (x-1) instead of X and Y instead of (y+1)
                System.out.println(number);
            }
        }
    }
}    
Whatever22
  • 26
  • 6
  • So, the problem lies within change of x-1 and y but few doubts : 1. why I am getting error if I use println() instead of sc.nextLine(), otherwise everything works fine. – Koushik Andhavarapu Aug 27 '22 at 06:50
  • I'm not sure that I inderstood you answer correctly, but if you meant a using System.out.println() instead of sc.nextLine(). then this will lead to an exception because after an instruction sc.nextInt() scanner keeps reading stdin and the method System.out.println() will give to the scanner the next value - an empty row. Read answers in this question, they are very useful: https://stackoverflow.com/q/13102045/5908736 – Whatever22 Aug 27 '22 at 07:31