'How to print user input in catch block or outside of catch block . User input is taken inside try block

Hi experts i am new to java programming ,apology if i am not able to explain my doubt clearly. This is practice question of hackerrank

Problem statement : take user input as number and classify it as it fit inside byte,short,int and long.

Input varaible T --> Number of test cases. n -> user input number, Below is my code also to remove NumberFormatException i used try-catch , i guess problem is at try-catch

    import java.io.*;

public class Solution {
    public static void main(String args[])throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        byte byteMin = -128;
            byte byteMax = 127;
            short shortMin = -32768;
            short shortMax =  32767;
            int intMin = -(int)Math.pow(2,31);
            int intMax = (int)Math.pow(2,31) -1 ;
            long longMin = -(long)Math.pow(2,63);
            long longMax = (long)Math.pow(2,63) -1 ; 
        while(T-- != 0) {
            int n;
            try {
            n = Integer.parseInt(br.readLine());
            }
            catch(NumberFormatException ex) {
                System.out.println(n+" can't be fitted anywhere.");
            }
            System.out.println(n+" can be fitted in:");
            if(n > byteMin && n < byteMax) {
                System.out.println("* byte");
            }
            if(n > shortMin && n < shortMax) {
                System.out.println("* short");
            }
            if(n > intMin && n < intMax) {
                System.out.println("* int");
            }
            if(n > longMin && n < longMax) {
                System.out.println("* long");
            }
        }
    }
}

Below was the input for the above code

5
-150
150000
1500000000
213333333333333333333333333333333333
-100000000000000

When i run it , it gave me variable n not defined. But when i defined variable n = 0 or -1 below was the output.

  -150 can be fitted in:
    * short
    * int
    * long
    150000 can be fitted in:
    * int
    * long
    1500000000 can be fitted in:
    * int
    * long
    -1 can't be fitted anywhere.
    -1 can be fitted in:
    * byte
    * short
    * int
    * long
    -1 can't be fitted anywhere.
    -1 can be fitted in:
    * byte
    * short
    * int
    * long

And the output i need is as below

   -150 can be fitted in:
    * short
    * int
    * long
    150000 can be fitted in:
    * int
    * long
    1500000000 can be fitted in:
    * int
    * long
    213333333333333333333333333333333333 can't be fitted anywhere.
    -100000000000000 can be fitted in:
    * long

What is going here in code when i assign a value to n as 0 or -1 can you explain it and how to solve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source