Skip to main content

Posts

Showing posts from November, 2021

How to Read PDF and Write into Text file in Java? PDF to Text file in Java

Copy PDF text and paste to Text file in Java In this article, we will seen how to create new text file and Extract text from PDF document to text file. We will use Apache pdfbox for extract PDF. For use Apache pdfbox we can use Maven project and include dependency or Crate Dynamic Web Project and add pdfbox JAR file. So in this we will use Dynamic Web Project.  Step 1 : Create new Dynamic Web Project in eclipse Go to File -> New -> Dynamic Web Project Create Java class. Step 2 : Add pdfbox JAR file in Project Click on below link and download JAR file. Download Apache Pdfbox JAR For include JAR into our project follow below steps : Click Right click on project -> Build Path - >  Configure Build Path Go to Libraries tab -> Click on Add External JARs button. Select Apache pdfbox jar. Click Apply and Close button. Now all set for extracting PDF store into text file. Step 3 : Java code for Extract PDF text to Text file import java.io.BufferedWriter; import java.io.Fi...

Sherlock and Anagrams HackerRank solution in Java | Anagrams string in Java

Java solution for Sherlock and Anagrams Problem Two strings are anagrams of each other if the letters of one string can be rearranged to form the other string. Given a string, find the number of pairs of substrings of the string that are anagrams of each other.  Example 1 : Input: s = abba Output : 4 The list of all anagrammatic pairs is [a, a], [ab, ba], [b, b] and [abb, bba] at positions [[0], [3]], [[0, 1], [2, 3]], [[1], [2]], [[0, 1, 2], [1, 2, 3]]. Example 2 : s = abcd No anagrammatic pairs exist in the second query as no character repeats. In this problem, we have to find that given String is anagram or not. But we have to find in sub string. So lets see solution and its explanation. import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.st...

How to use Authentication and Authorization in Spring Security | Role based Authorization

Role based Authorization (Admin and Other User) and Permissions in Spring Security with Spring Boot In this article, we will see how we can achieve Authentication using inMemoryAuthentication and role based Authorization in Spring Security. We perform Authentication and Authorization with Spring Boot application that we already seen in older articles. First Refer below articles related Spring Boot CRUD operation with Rest API and Thymeleaf. Spring Boot CRUD (Create, Read, Update, Delete) operation with Rest API, JPA and MySql  Spring Boot Crud Operation with Thymeleaf, MySql, JPA and Rest API   For enable security in spring, first we have to add below dependency in pom.xml file. Step 1 : Add spring-security dependency in pom.xml file <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-security</artifactId> </dependency> Step 2 : Create new Java class and extends wit...

Two Strings HackerRank solution in Java with Explanation | Programming Blog

Find common substring in Java from two String Problem Description : Given two strings, determine if they share a common substring. A substring may be as small as one character.  Example 1 : s1 = "and" s2 = "art" These share the common substring 'a'. So answer will be YES Example 2 : s1 = "Hi" s2 = "World" Answer : NO See full description on Hackerrank : Two Strings Problem Description Lets see solution : We will see two solutions : Using Map Using Character Array In this problem, first approach pass all test cases but second approach gives Time Limit Error. but for learning purpose we will see that also. So lets turn on learning mode. Solution 1 : Finding common substring using Map in Java import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution {     static String twoStrings (String s1, String s2) {          ...

Sorting: Bubble Sort HackerRank solution in Java | Programming Blog

Problem Description : Given an array of integers, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following three lines: Array is sorted in numSwaps swaps., where numSwaps is the number of swaps that took place. First Element: firstElement, where firstElement is the first element in the sorted array. Last Element: lastElement, where lastElement is the last element in the sorted array. See full description on HackerRank : Problem Description : Bubble Sort   Input and Output : Input : a = [6, 4, 1] Output : 1       [4,6,1] 2       [4,1,6] 3       [1,4,6] Array is sorted in 3 swaps.   First Element: 1   Last Element: 6   In this problem we already have bubble sort code. we have to simply print swap count, first element and last element after swap. So lets see solution. import java.io.*; import java.math.*; import java.security.*...