Java Recursive Program to Compute the Sum of Digits in a given Integer

``````public class SumOfElem {
static int sum=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
int no=3850;
int val=getValue(no);
System.out.println("The Total is:"+val);
}
public static int getValue(int a)
{

if(a<1)
{
return sum;
}else
{
int rem=a%10;
sum =sum+rem;
getValue(a/10);
}
return sum;
}

}``````

O/P:

Feel free to comment and share if liked ðŸ™‚

Bubble Sort Implementation In Java Program

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

Properties

• Worst case performance O(n^2)
• Best case performance O(n)
• Average case performance O(n^2)
``````public class BubbleSort {

/**
* @param args
*@author Shubham Shah
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {
// TODO Auto-generated method stub
//int [] arr ={9,2,1,8,0,3,34,12,342,234,4545,5,6,67,87,56,12,23,45,67,78,89,9,98,87,65,45,34,789,45,46,47,48,49,59,58,57,55,54,52,53,51,9999};
int [] arr ={3,2,1,0};
long startTime = System.currentTimeMillis();
String reqTime= new	java.text.SimpleDateFormat("MM-dd-yyyy.HH:mm:ss.SSS").format(new java.util.Date());
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
System.out.println("Poistion of i:"+i+"| Postion of j:"+j);
if(arr[i]>arr[j])
{
System.out.println("Swapping "+arr[i]+": & :"+arr[j]);
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}

for(int k=0;k<arr.length;k++)
System.out.println("OP>"+arr[k]);

}

}``````

Output :

Feel free to write comments and share if you like ðŸ™‚

Reading and writing jar files using java

Hi Friends ,

I’m back with another interesting tutorial , today i’ll telling how to read a jar file and write the jar file to destination folder.

You can also say copying jar file from one location to another /destination location.

So first find the code below :

So now what i’m doing is reading the “sourceLocation” checking whether its a file if yes calling my method to copy that file to “destinationLocation” else if its folder i’m going inside the folder scanning for jar file and calling the method passing that file as argument.

So in the method “copyJarFile” takes two argument the source jar file and other the destination folder where the source file must be copied here we are using the

`JarOutputStream jos = new JarOutputStream(new FileOutputStream(destFile));`

JarOutputStream for taking jar file as inputstream

then

`Enumeration<JarEntry> entries = jarFile.entries();`

inorder get the entries inside it and writing jos.

So using while loop all entries are written and then we are closing stream and flushing out .

Feel free to comment for any doubts , also if you like it please share as sharing is caring ðŸ™‚

Simple java program to read files inside jar files and run those files .

Hi Everyone ,

Today we will be learning a very interesting topic where in your most of questions related to

`How to get a path to a resource in a Java JAR file?`
`Running the files inside the jar file once packaged?`

So the main thing is firstly you need to add your resource folder to your build.

In order to that

1)Right click on the project

2)Project properties

3)Search build and add your resource folder in source

4)Apply and close

Now once cleanÂ  and build the project.

Now here is the tricky part , when you export your project as jar and run the program you will still get the java.io null pointer / file not found exception i.e you’ve use the

`class.getClassLoader().getResource("index.html");`

Here “index.html” is a file inside my resource folder.

What really happens at run time is we do not the get path as its inside the jar and not on the user’s file system.

So what i’ve implemented is

`class.getClassLoader().getResourceAsStream("index.html");`

I’ve got it as stream then read the file lineÂ  by line and created a folder(i.e resourcess) on the user system and copied the file.

So now we have a real relative path available so we can do stuff , here i wanted the file (i.e index.html) to be opened on the default browser of operating system.

And now the source code.

Please feel free to ask any doubts and questions.

If it solve your problem please share ðŸ™‚ as sharing is caring.

Building a docker image using simple jar File.

Hi Guys Today is a very Special tutorial of how to create a docker image from scratch.

A Simple way to create docker image.

The workflow of Dockerfile and building is very simple. It goes like this….

1. Docker first run’s the container that you mentioned us FROM (in our case its Java 8). So docker first pulls down that image ,and run’s it.
2. It then looks for the first instruction after that, and after executing that, it does something similar to #docker commit, so that it saves a layer with what is done till that instruction.
3. Docker run’s a new container with the last committed image (ie. The image created from the previous instruction step in the Dockerfile.Â TheÂ Running in Â  type of messages during the build command showed in the above example indicates this.)
4. It then executes the next instruction in the Dockerfile, and again does a commit and creates another image. So that the very next instruction is executed inside this new image committed. And this process continues till the very last instruction in the Dockerfile.

Feel free to comment for any doubts.

If you like please share and subscribe ðŸ™‚

Using Font Awesome Stack Property and Printing Images in Console

Hi Guys,

I’ll be showing how to using the stack property of font awesome inorder to show numbers inside the icons.

It can be handy in manyÂ situations like showing the notification icon with no of notifications.

The second thing will be printing images in the console.

I’d a rare scenario where i needed the images received is right or wrong so instead of displaying i choose to print on console.

Also find the github code here :

Feel free to comment and share ðŸ™‚

Add Multiple Markers to Map from user input

Hello Everyone ,

Recently i’ve been in a situation where in i needed to drop markers to location on map wherein the location is given by the user.

It was a unique situation as in the markers should be updated in real time to map with retaining the previous markers.

After digging to various stack overflow posts and googling found that there no easier way to it.So wrote a simple java script code which adds the markers to map in real time also retaining the previous location

And bang done.

Feel free share and ask Questions.