Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Java Static Initializer Block problem solution

YASH PAL, 31 July 202422 October 2025

Hackerrank Java Static Initializer block solution – In this HackerRank Java Static Initializer Block problem statement, we need to develop a program that accepts two lines of input. the first line of input is the breadth of the parallelogram and the second line of input is the height of the parallelogram.

if both input values are greater than zero then we need to print the area of parallelogram otherwise print the java. lang.exception: breadth and height must be positive.

Static initialization blocks are executed when the class is loaded, and you can initialize static variables in those blocks.

It’s time to test your knowledge of Static initialization blocks. You can read about it here.

You are given a class Solution with a main method. Complete the given code so that it outputs the area of a parallelogram with breadth B and height H. You should read the variables from the standard input.

If B <= 0 or H <= 0 , the output should be “java.lang.Exception: Breadth and height must be positive” without quotes.

Input Format

There are two lines of input. The first line contains B: the breadth of the parallelogram. The next line contains H: the height of the parallelogram.

Constraints

  • -100 <= B <= 100
  • -100 <= H <= 100

Output Format

If both values are greater than zero, then the main method must output the area of the parallelogram. Otherwise, print “java.lang.Exception: Breadth and height must be positive” without quotes.

Java Static Initializer Block problem solution | Hackerrank

Static Initializer block problem solution in java7.

static boolean flag = true; 
static int B,H;

static{

Scanner scan = new Scanner(System.in);
B = scan.nextInt();
scan.nextLine();
H = scan.nextInt();
scan.close();
if(B>0 && H>0){
    flag = true;

}else if(B<=0 || H<=0){
    flag=false;
    System.out.println("java.lang.Exception: Breadth and height must be positive");
}
}

Second solution

static Scanner scan = new Scanner(System.in);
static int B = scan.nextInt();
static int H = scan.nextInt();
static boolean flag;
static {
    if(B<=0 || H <=0){
        flag = false;
        System.out.println("java.lang.Exception: Breadth and height must be positive");
    } else {
        flag = true;
    }
}
coding problems solutions Java Programming Tutorials HackerRank

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes