Sunday, July 15, 2018

Given Strings are anagrams or not using java



What is String Anagram?

"An anagram is a type of word, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.

for Example: 'java' can be rearranged as - 'vaaj' or 'vjaa' -all characters are equal in both the words. So this is called string anagram.

Now we will see how to write a java program to check whether given strings are anagram or not.


package com.siva;

import java.util.Arrays;

public class StringAnagramTest {
 
 
  public boolean isStringAnagram(String s1, String s2){
   char[] charArray1 = s1.toCharArray();
   char[] charArray2 = s2.toCharArray();
   Arrays.sort(charArray1);
   Arrays.sort(charArray2);
   boolean stringAnagram = Arrays.equals(charArray1, charArray2);
   return stringAnagram;
  }
  
  public static void main(String[] args) {
   StringAnagramTest test = new StringAnagramTest();
    String str1 ="test";
    String str2 ="sett";
  boolean stringAnagram = test.isStringAnagram(str1, str2);
   if(stringAnagram){
    System.out.println("Given Strings["+str1+"] and ["+str2 +"] are anagrams");
   }else{
    System.out.println("Given Strings["+str1+"] and ["+str2 +"] are not anagrams");
   }
}

}




We can provide any value as input for both the strings.

Detailed Explanation:

Step 1: Convert given Strings into character array.

Step 2: Sort the converted character array using Arrays.sort

Step 3: compare both the strings using Arrays.equals

There are different ways to implement the same logic by writing our own sorting and equals methods. this is simple way to find the result.

output:



Given Strings[test] and [sett] are anagrams


Thursday, March 1, 2018

Java Program to check whether given word is Palindrome or not


This post will explain us, how to implement palindrome logic with different ways, using java.

public class WordPalindrome {
  public static void main(String[] args) {
   String word="LIRIL";
   boolean isPalin = isPalindromeOne(word);
   if(isPalin){
    System.out.println("Given word ["+word+"] is palindrome");
   }else{
    System.out.println("Given word ["+word+"] is not palindrome");
   }
   word = "Hello";
   isPalin = isPalindromeOne(word);
   if(isPalin){
    System.out.println("Given word ["+word+"] is palindrome");
   }else{
    System.out.println("Given word ["+word+"] is not palindrome");
   }
   
  }


  public static boolean isPalindromeOne(String word){
   boolean isPalindrome = false;
   //first way of checking
   char[] array = word.toCharArray();
   char[] newArray = new char[array.length];
   for (int i = 0; i < array.length; i++) {
  char c = array[array.length-i-1];
  newArray[i] =(char)c;
 }
   if(word.equalsIgnoreCase(String.valueOf(newArray))){
    isPalindrome = true;
   }
   //second way of checking
   String reverse ="";
   for (int i = 0; i < word.length(); i++) {
  char c= word.charAt(i);
  reverse = c+reverse;
 }
   if(reverse.equalsIgnoreCase(word)){
    isPalindrome = true; 
   }
   
   
 
   return isPalindrome;
  }
  
  
  
}


Saturday, February 10, 2018

Jersy Rest Webervice with eclipse and Weblogic 12c or tomcat


How to work with jersy rest webservice using eclipse and weblogic server/tomcat

Step 1: Create a dynamic web project in eclipse and provide project name





Step 2: open web.xml and add below configuration code for rest webservices.



  jersyRestService
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
      
        Jersey Web Application
        com.sun.jersey.spi.container.servlet.ServletContainer
    
    
        Jersey Web Application
        /rest/*
    



Step 3: Create one file name called weblogic.xml and add the below code.



12.2
    
        jax-rs
        2.0
        2.22.1.0
        false
    



Step 4: Download rest api related jar -http://repo1.maven.org/maven2/com/sun/jersey/jersey-archive/1.19.1/jersey-archive-1.19.1.zip Unzip the same and place the jar inside lib folder of the project.


Step 5: Create a class name called RestServiceAPI.java add below code.

package com.siva.restservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
/**
 * 
 * @author Siva
 *
 */

@Path("/helloRest")
public class SimpleRestService {
	
	
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String getDetails(){
		System.out.println("This is Get request");
		return "Hello Welcome to Rest webservice";
	}
	@GET
	@Path("/getDetail/{requestParam}")
	@Produces(MediaType.TEXT_PLAIN)
	public String getDetailsWithParameter(@PathParam("requestParam") String requestParam){
		System.out.println("USer request paramater["+requestParam +"]");
		System.out.println("This is Get request");
		return "Hello Welcome to Rest webservice you have given input as -"+requestParam;
	}

}



Step 6: Create one config class called- ApplicationConfig.java
Right click on src-> Webservice->RestWebservice->select Classname- which class needs to be configured in ApplicationConfig Click Next-> Finish

package rest.application.config;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
 * 
 * @author Siva
 *
 */
@ApplicationPath("resources")
public class ApplicationConfig  extends Application{

	public Set> getClasses(){
		return getRestClasses();
	}
	
	private Set> getRestClasses(){
	  Set> resources = new HashSet>();	
	  resources.add(com.siva.restservice.SimpleRestService.class);
	  return resources;
	}
	
}



Step 7: This is very important step

Run the weblogic server and open the console
http://localhost:7001/console provide username and password and login the same into weblogic server admin console

Step 8: Click on the Deployments- the install the jax-rs-2.0.war, which is inside-
Oracle_home/wlserver/common/deployable-libraries
If you are working on jsr311 then deploy jsr311-api-111.war which is available on same path.


For Tomcat Deployment Step 3 , 6,7,8 not required

Right click on the Project and Run AS - server- select appropriate server and deploy the same.

if you want to test in browser use-
http://localhost:8080/jersyRestService/rest/helloRest
http://localhost:8080/jersyRestService/rest/helloRest/getDetail/helloRequest


Step 9: Create rest client class which get the test results for rest webservice
package com.siva.restservice.test;

import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
/**
 * 
 * @author Siva
 *
 */
public class RestServiceClient {
	
	public static void main(String[] args) {
		String uri = "http://localhost:8080/jersyRestService/rest/helloRest";
		try{
			
			Client client = Client.create();
			WebResource resource = client.resource(uri);
			String response = resource.type(MediaType.TEXT_PLAIN).get(String.class);
			System.out.println("Response from Rest webservice["+response+"]");
			String request = "GetRestWithParam";
			uri = uri.concat("/getDetail/").concat(request);
			resource = client.resource(uri);
			String response1 = resource.type(MediaType.TEXT_PLAIN).get(String.class);
			System.out.println("Response from Rest webservice with parameter["+response1+"]");
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}

}


Step 10: You can see the output as below

Response from Rest webservice[Hello Welcome to Rest webservice]
Response from Rest webservice with parameter[Hello Welcome to Rest webservice you have given input as -GetRestWithParam]


Thank you very much for viewing this post. If you like, don't forget to share/comment.


AddToAny

Contact Form

Name

Email *

Message *