Saturday, 23 March 2019

Hackerrank SQL Solution-Higher Than 75 Marks

Problem:-
Query the Name of any student in STUDENTS who scored higher than  Marks. Order your output by the last three charactersof each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.
Input Format
The STUDENTS table is described as follows:The Name column only contains uppercase (A-Z) and lowercase (a-z) letters.
Sample Input
Sample Output
Ashley
Julia
Belvet
Explanation
Only Ashley, Julia, and Belvet have Marks > . If you look at the last three characters of each of their names, there are no duplicates and 'ley' < 'lia' < 'vet'.

Solution:-
Select Name
From STUDENTS 
Where Marks>75
Order By SUBSTRING(Name, -3, 3) ,ID ;

What is a NullPointerException, and how do I fix it?

When you declare a reference variable (i.e. an object) you are really creating a pointer to an object. Consider the following code where you declare a variable of primitive type int:
int x;
x = 10;
In this example, the variable x is an int and Java will initialize it to 0 for you. When you assign it to 10 in the second line your value 10 is written into the memory location pointed to by x.
But, when you try to declare a reference type something different happens. Take the following code:
Integer num;
num = new Integer(10);
The first line declares a variable named num, but, it does not contain a primitive value. Instead, it contains a pointer (because the type is Integer which is a reference type). Since you did not say as yet what to point to Java sets it to null, meaning "I am pointing at nothing".
In the second line, the new keyword is used to instantiate (or create) an object of type Integer and the pointer variable num is assigned this object. You can now reference the object using the dereferencing operator . (a dot).
The Exception that you asked about occurs when you declare a variable but did not create an object. If you attempt to dereference num BEFORE creating the object you get a NullPointerException. In the most trivial cases, the compiler will catch the problem and let you know that "num may not have been initialized" but sometimes you write code that does not directly create the object.
For instance, you may have a method as follows:
public void doSomething(SomeObject obj) {
   //do something to obj
}
In which case you are not creating the object obj, rather assuming that it was created before the doSomething method was called. Unfortunately, it is possible to call the method like this:
doSomething(null);
In which case obj is null. If the method is intended to do something to the passed-in object, it is appropriate to throw the NullPointerException because it's a programmer error and the programmer will need that information for debugging purposes.
Alternatively, there may be cases where the purpose of the method is not solely to operate on the passed in object, and therefore a null parameter may be acceptable. In this case, you would need to check for a null parameter and behave differently. You should also explain this in the documentation. For example, doSomething could be written as:
/**
  * @param obj An optional foo for ____. May be null, in which case 
  *  the result will be ____.
  */
public void doSomething(SomeObject obj) {
    if(obj != null) {
       //do something
    } else {
       //do something else
    }
}

Java Static Initializer Block Example- Hackerrank Java Static Initializer Block solution

Problem:-
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  and height . You should read the variables from the standard input.
If  or  , 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 : the breadth of the parallelogram. The next line contains : the height of the parallelogram.
Constraints
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.
Sample input 1
1
3
Sample output 1
3
Sample input 2
-1
2
Sample output 2
java.lang.Exception: Breadth and height must be positive

Solution:-
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;


public class Solution {
static int B=0;static int H=0;static boolean flag=true;
static{
Scanner sc = new Scanner(System.in);
B=sc.nextInt();
H=sc.nextInt();
if(B<=0||H<=0)
{
flag=false;
System.out.println("java.lang.Exception: Breadth and height must be positive");
}
};


public static void main(String[] args){
        if(flag){
            int area=B*H;
            System.out.print(area);
        }
        
    }//end of main

}//end of class





“cannot find symbol - class Scanner” error

As the OP is a new beginner to programming, I would like to explain more.
You will need this line on the top of your code in order to compile:
import java.util.Scanner;
This kind of import statement is very important. They tell the compile of which kind of Scanner you are about to use, because the Scanner here is undefined by anyone.
After a import statement, you can use the class Scanner directly and the compiler will know about it.
Also, you can do this without using the import statement, although I don't recommend:
java.util.Scanner scanner = new java.util.Scanner(System.in);
In this case, you just directly tell the compiler about which Scanner you mean to use.

Friday, 22 March 2019

Detect Session In JSP page of java web application

At the beginning of each page you can do this:
String username = (String)session.getAttribute("USER");
if(username==null) { // session expired
// FORWARD WITH ANY WAY YOU WANT
// I usually use the Request Dispatcher class.
RequestDispatcher rd = context.getRequestDispatcher("/demo/inner.jsp");
rd.forward(request,response);
}

Error While embed the video in Your website page

Error:- Refused to display '<URL>' in a frame because it set 'X-Frame-Options' to 'sameorigin Solution:- if ...