We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Scanner scan = new Scanner(System.in);
B = scan.nextInt();
H = scan.nextInt();
try{
if(B<=0 || H<=0){
flag = false;
throw new Exception("Breadth and height must be positive");
}
}catch(Exception e){
System.out.println(e);
}
Instead of using or you can use multiplication i.e, if bh<0 the string is printed. This is better because, if we extend the code this format will be better to debug.
I found twice repeating "java.lang.Exception:" in your code.
Line 17 should be corrected as :
throw new Exception("Breadth and height must be positive");
static int B,H;
static boolean flag = true;
static Scanner scan = new Scanner(System.in);
static{
B = scan.nextInt();
scan.nextLine();
H = scan.nextInt();
scan.close();
if(B<= 0 || H<=0){
flag = false;
System.out.println("java.lang.Exception: Breadth and height must be positive");
}
No You can't do that because we need to reference the variables in main method,as main method is non-editable.so the only option you have that while compiling, when JVM loads (.class) file in the memory intialize all those variables at the same time that can be done when you declare variables as static.
staticbooleanflag=true;staticScannerin=newScanner(System.in);staticintB=in.nextInt();staticintH=in.nextInt();static{if(B>0&&H>0)flag=true;else{flag=false;System.out.println("java.lang.Exception: Breadth and height must be positive");}}
Note: variables inside the static block is not static.
staticbooleanflag=true;staticScannerin=newScanner(System.in);staticintB=in.nextInt();staticintH=in.nextInt();static{if(B>0&&H>0)flag=true;else{flag=false;System.out.println("java.lang.Exception: Breadth and height must be positive");}}
paste the same code,complied and tested
and let me know!
No You can't do that because we need to reference the variables in main method,as main method is non-editable.so the only option you have that while compiling, when JVM loads (.class) file in the memory intialize all those variables at the same time that can be done when you declare variables as static.
It works vs this
static Scanner input = new Scanner(System.in);
static boolean flag = true;
static int B = input.nextInt();
static int H = input.nextInt();
static{
try{
if(B <= 0 || H <= 0){
flag = false;
throw new Exception("Breadth and height must be positive");
}
}catch(Exception e){
System.out.println(e);
}
why are we defining the B and H two times like first in the starting with static keyword and again in scanner? scan.nextInt() is also saying the same thing that B is of integer type, Please explain
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Java Static Initializer Block
You are viewing a single comment's thread. Return to all comments →
static boolean flag=true;
static int B,H;
static {
}
Instead of using or you can use multiplication i.e, if bh<0 the string is printed. This is better because, if we extend the code this format will be better to debug.
we should not use bh<0 condition because if b and h both are negative, then your code will set flag as true which is incorrect
we cant use this as if both b & h are negative the condition will get true which should not be happen
if both b & h are negative the condition will get false and it throw the exception
why do we need flag in this to true can we use and operator insted of or .................................please help
I found twice repeating "java.lang.Exception:" in your code. Line 17 should be corrected as : throw new Exception("Breadth and height must be positive");
static int B,H; static boolean flag = true; static Scanner scan = new Scanner(System.in); static{ B = scan.nextInt(); scan.nextLine(); H = scan.nextInt(); scan.close();
}
This works fine.
Code is working but how you are calculating the area .
Area is calculated in the main method. Expand the code and you will find it! :)
thank you
cool...
Why can't we put boolean flag = true; and int B,H; in the static block??
No You can't do that because we need to reference the variables in main method,as main method is non-editable.so the only option you have that while compiling, when JVM loads (.class) file in the memory intialize all those variables at the same time that can be done when you declare variables as static.
Note: variables inside the static block is not static.
why are we using the boolean variable flag?
alredy given flag in main function as a conditon for if so we should use flag
how to access the main class??
Click on arrow you will find the full part of main class.
i have used the same code, still getting errors...
.java:36: error: class, interface, or enum expected public static void main(String[] args){ ^ .java:39: error: class, interface, or enum expected System.out.print(area); ^ .java:40: error: class, interface, or enum expected } ^
I'm not getting this error...plz help me out.
System.out.print(area); ?????
paste the same code,complied and tested and let me know!
i did the same thing but got an error class,enum or interface expected.pls help me
i also did exactly same but i am getting error
Do not close class before main. There is closing parenthesis for class after main.
Thanks a ton! It Worked for me.
its showing semicolan expected
thank u
I tried doing it that way but it went wrong, so I had to take the:
out of the block.
Why is it?
No You can't do that because we need to reference the variables in main method,as main method is non-editable.so the only option you have that while compiling, when JVM loads (.class) file in the memory intialize all those variables at the same time that can be done when you declare variables as static.
Thanks, that helps a lot to understand how it works
It works vs this static Scanner input = new Scanner(System.in); static boolean flag = true; static int B = input.nextInt(); static int H = input.nextInt();
static{ try{ if(B <= 0 || H <= 0){ flag = false; throw new Exception("Breadth and height must be positive"); } }catch(Exception e){ System.out.println(e); }
}
end class erorr
why are we defining the B and H two times like first in the starting with static keyword and again in scanner? scan.nextInt() is also saying the same thing that B is of integer type, Please explain