Drop Down

Monday, February 4, 2019

Reverse String

package interview;

import java.util.Scanner;

public class ReverseString {

public static void main(String[] args)
{
String original;
String rev;

System.out.println("Enter a String");
Scanner kb = new Scanner(System.in);
original = kb.nextLine();

 
  //***********Using InBuild methods
 
  StringBuffer sb = new StringBuffer(original);
  rev = sb.reverse().toString();
  System.out.println(rev);
 

}

}

Find No. of Words: No. of characters: List to duplicate Words from text File

package test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/*
No. of Words:
No. of characters:
List to duplicate Words:
D:\work\Java Work Space\files\amit.txt
*/
public class Count_Duplicate_Words_From_File
{
public static void main(String[] args) throws IOException, FileNotFoundException
{
  BufferedReader br = new BufferedReader(new FileReader("D:\\work\\Java Work Space\\files\\amit.txt"));
  StringBuffer sb = new StringBuffer();
  String s = br.readLine();
  while(s!= null)
  {
  //sb = sb.append(s); //----->White ko hatna hetu
  sb = sb.append(s.trim());
  sb = sb.append(" ");
  s = br.readLine();
  }
  br.close();
  String[] words = sb.toString().split(" ");
  wordCount(words);
  charCount(words);
  DupWords(words);
 
}

private static void DupWords(String[] words)
{
  Map <String,Integer> map = new HashMap<>();
  for(String s : words)
  {
  if(!map.containsKey(s))
  {
  map.put(s, 1);
  }
  else
  {
  map.put(s, map.get(s)+1);
  }
  }
  System.out.println("List to duplicate Words:");
  for(Map.Entry<String,Integer> entry : map.entrySet())
  {
  if(entry.getValue()>1)
  System.out.println(entry.getKey()+" : "+entry.getValue());
  }

}

private static void charCount(String[] words)
{
char[] ch;
int count = -1;
  for (String s : words)
  {
for(int i = 0;i<s.length();i++)
{
count++;
}
count++;
  }
  System.out.println("No. of characters: "+count);

}

private static void wordCount(String[] words)
{
  System.out.println(Arrays.toString(words));
  System.out.println("No of Words: "+words.length);

}
}

Sunday, February 3, 2019

CharToString

public class CharToStringExample {

       public static void main(String args[]) {
              char ch = 'U';

              // char to string using Character class
              String charToString = Character.toString(ch);
              System.out.println("Converting Char to String using Character class: " + charToString);

              // char to String using String concatenation
              String str = "" + ch;
              System.out.println("Converting Char to String using String concatenation: " + str);

              // char to String using anonymous array
              String fromChar = new String(new char[] { ch });
              System.out.println("Converting Char to String using anonymous array: " + fromChar);

              // char to String using String valueOf
              String valueOfchar = String.valueOf(ch);
              System.out.println("Converting Char to String using String valueOf: " + valueOfchar);

       }

}

Output:
Converting Char to String using Character class: U
Converting Char to String using String concatenation: U
Converting Char to String using anonymous array: U
Converting Char to String using String valueOf: U


Read more: https://javarevisited.blogspot.com/2012/02/how-to-convert-char-to-string-in-java.html#ixzz5eT208uoa

Friday, February 1, 2019

SQL Queries

--=====================Nth HIGHEST SALARY=======================
--Highest salary
select max(salary) from employees;
--2nd highest salary
select max(salary) from employees where salary < (select max(salary) from employees);
--Nth Highest Salary
SELECT EMPLOYEE_ID,FIRST_NAME, LAST_NAME,salary
FROM EMPLOYEES e1
WHERE 3 = (SELECT COUNT(DISTINCT(SALARY)) FROM
EMPLOYEES e2 WHERE e2.SALARY > e1.SALARY);
--===============================================================
--===================DELETE DUPLICATE ROWS=======================
--===============================================================
delete from emp where rowid in(
select rowid from(
select k.*,ROW_NUMBER() OVER(partition by id order by id)R ,rowid
from emp k )
where R!=1);
--Explanation
select * from emp; --13 rows
select k.* from emp k; --13 rows
select id, count(1) from emp group by id; -- to get duplicates
select id, count(1) from emp group by id having count(*)>2; --to get duplicates>2
select id, count(1) from emp group by id;
select distinct * from emp; -- 3 rows
--provide row number to it , OK
select k.*,ROW_NUMBER() OVER(order by id) from emp k; --provided row number to each rows
--Now partition table based on id
select k.*,ROW_NUMBER() OVER(partition by id order by id) from emp k; --partition of table based on id
--Here when we partition for each new ID counting starts from 1.
--select rownum, rowid
select k.*,ROW_NUMBER() OVER(partition by id order by id)R ,rowid from emp k;
---
delete from emp where rowid in(
select rowid from(
select k.*,ROW_NUMBER() OVER(partition by id order by id)R ,rowid
from emp k )
where R!=1);
--===============================================================
--===========find employees hired in last n months ==============
--===============================================================

select first_name,hire_date from employees where
to_char(hire_date,'DD')<15 and to_char(hire_date,'MM')<may;

--===============================================================
--===================*********************=======================
--===============================================================



--===============================================================
--===================*********************=======================
--===============================================================



--===============================================================
--===================*********************=======================
--===============================================================

Monday, January 28, 2019

Struts_App1

--web.xml
-------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Struts1_HelloWorld</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <servlet-name>action</servlet-name>
       <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
         <init-param>
  <param-name>config</param-name>
  <param-value>/WEB-INF/struts-config.xml</param-value> 
  </init-param>
  <load-on-startup>1</load-on-startup>  
  </servlet>
  <servlet-mapping>
        <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>

</web-app>
----------------------
--Index.jsp
---------------------
<%@taglib uri="http://struts.apache.org/tags-html" prefix ="html"%>
<html:errors/>
<h1>Welcome to World</h1>
<html:form action ="hello">
Name: <html:text property="name"/>
Roll: <html:text property="roll"/>
<html:submit value ="Say Hello"/>
</html:form>
---------------------
--FormBankUp.java
---------------------
package bean;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
//This is bean class which extends ActionForm, //
public class FormBackUp extends ActionForm
{
private int roll;
private String name;

public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
ActionErrors ae = new ActionErrors();
if(name.equals(""))
{
ae.add("name", new ActionMessage("nameError"));
//return ae;
}
/*if("".equals(getRoll() ) )
{
ae.add("roll", new ActionMessage("rollError"));
//return ae;
}*/
return ae;
}
}

-----------------------
--HelloController.java
-----------------------
package bean;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.struts.action.*;
public class HelloController extends Action
{//This similar to controller classes ie. Servlets service()
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ServletRequest request, ServletResponse response)
throws Exception 
{
String name = request.getParameter("name");
int roll = Integer.parseInt(request.getParameter("roll"));
System.out.println("Name: "+name);
System.out.println("Roll: "+roll);
request.setAttribute("res","Hello......"+name);
return mapping.findForward("success");
}
}

---------------------
--Success.jsp
---------------------

<%=request.getAttribute("res") %>>

--------------------
--struts-config.xml
--------------------
<?xml version="1.0" encoding="UTF-8"?>
<struts-config>
<form-bean>
<form-bean name="HF" type ="bean.FormBackUp"/> <!-- HF if ref. to FormBackUP -->
</form-bean>
<action-mapping>
<action path="/hello" name="HF" index="/index.jsp" type="bean.HelloController">
<forward name="success" path="/Success.jsp"/>
</action>
</action-mapping>
<message-resources parameter="bean/messages"/>


</struts-config>
--------------------------
--messages.properties
--------------------------
nameError = <font color='red'> Please Enter Name</font>
rollError = <font color='red'> Please fill Roll No.</font>

========================================================================

Tuesday, January 22, 2019

Google Drive Link: Core Codes

Multithreading:: InterProcess Communication

package multithreading;

public class ThreadA
{
public static void main(String[] args) throws InterruptedException
{
ThreadB b = new ThreadB();
b.start();
synchronized (b)
{
  b.wait();
}
System.out.println("Total: "+ b.sum);
}
}

class ThreadB extends Thread
{
int sum =0;
public void run()
{
for(int i=1;i<100;i++)
{
sum = sum+i;
}
synchronized(this)
{
this.notify();
}
}

}

Java 8 Notes Pics