-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

About me

Hello

I'mITEngineer8995

Spatial Data Specialist

Do you want all of your codes in one place?? Then you are landed on the right blog... Here you will find varieties of codes of various languages starting from noob to advance with their samples. Do learn, execute and share your knowledge with others. Do visit my YouTube Channel for execution of these codes .

Experience

Video Clone Detector using Hadoop

2017-2018

A detector which differentiates copied videos from the original ones using Hadoop.

Portfolio

Java Program To Convert Char Array To String

 


Java Program To Convert Char Array To String

In this Java Program, we convert the char array to String. 

Array is a data structure consisting of collections of elements each identified by atleast one array index or key. 

E.g. If you want to store 5 numbers in an array,

        datatype arrayname [arraysize];

         int a[5];

Logic : 1. Using String object: 
                 String str = new String(ch);

             2. Using ValueOf method.
                 String str2 = String.valueOf(ch);

Step 1 : Create an array of char and store the characters in it.

Step 2 : Create an object of String as str and store the characters of ch in it.

Step 3 : Output the object str.

Step 4 : Create object of String str and store characters in it by using ValueOf() method.

Step 5 : Output the object str.
              
Program :

public class CharArrayToString 
    {
        public static void main(String args[])
        {
        //Method 1 : Using String object
         char[] ch = { 'h', 'a','v','e','á','g', 'o', 'o', 'd', 'd', 'a', 'y'};
         String str = new String(ch);
         System.out.println(str);
         
         //Method 2 : Using valueOf method
         String str2 = String.valueOf(ch);
         System.out.println(str2);
        }
}

Output :

haveágoodday
haveágoodday

For execution of the same do visit my YouTube Channel :


Java Program To Reverse The Array

 


Java Program To Reverse The Array

In this Java Program, we reverse the array. In this program if a user enters numbers 1, 2, 3, 4, 5 then the program will reverse the array and the elements will be 5, 4, 3, 2, 1.  

Array is a data structure consisting of collections of elements each identified by atleast one array index or key. 

E.g. If you want to store 5 numbers in an array,

        datatype arrayname [arraysize];

         int a[5];

Step 1 : Import java.util.Scanner Class

Step 2 : Create and initialize variable i and j to 0, counter and temp.

Step 3 : Create an array named number and store value 100 in it.

Step 4 : Create the object of the Scanner class

Step 5 : Ask the user to the enter the number of elements he want to enter.

Step 6 : Store the user entered numbers in the variable counter

Step 7 : Apply for loop where i is equal o 0, i should be less than counter and increment i.
              This loop stores all the elements that we enter in an array number. First element is at number[0], second at number[1] and so on.

Step 8 : Print the enter array element by adding  1 int the i.

Step 9 : Store the user entered number in the array of number[i]

Step 10 : Decrement i by 1 and store it in j. Here we are writing the logic to swap first element with last element, second last element with the second element and so on. On the first iteration of the while loop i is the index of first element and j is the index of last. On the second iteration i is the index of the second and j is the index of the second last.

Step 11 : Initialize i to 0

Step 12 : Close the Scanner class to avoid any memory leak.

Step 13 : Apply while loop where i should be less than j

Step 14 : Store array number[i] in the temp variable

Step 15 : Store array of number[i] in array of number[j]

Step 16 : Store array of number [j] in variable temp.

Step 17 : Increment i & decrement j

Step 18 : Print reversed array

Step 19 : Apply for loop where i should be equal to 0, i should be less than counter and increment i

Step 20 : Print the array of number [i]

Program :

import java.util.Scanner;

public class ReverseArray 
    {
        public static void main(String args[])
        {
            int counter, i = 0, j = 0, temp;
            int number[] = new int[100];
            Scanner sc = new Scanner(System.in);
            System.out.print("How many elements you want to enter:");
            counter = sc.nextInt();
            
            for(i = 0; i < counter; i++)
            {
                System.out.print("Enter Array Element"+(i + 1)+": ");
                number[i] = sc.nextInt();
                
            }
            j = i - 1;
            i = 0;
            sc.close();
            while(i < j)
            {
                temp = number[i];
                number[i] = number[j];
                number[j] = temp;
                i++;
                j--;
                
            }
            
            System.out.print("Reversed array:");
            for(i = 0; i < counter; i++)
            {
                
                System.out.print(number[i]+" ");
            }
            
            
        }
    
}

Output: 

How many elements you want to enter:5
Enter Array Element1: 11
Enter Array Element2: 22
Enter Array Element3: 33
Enter Array Element4: 44
Enter Array Element5: 55
Reversed array:55 44 33 22 11

For execution of the same do visit my You Tube Channel :



Java Program To Sum the elements of the Array Entered By The User


Java Program To Sum the elements of the Array Entered By The User

In this Java Program, we sum up all the elements of the array entered by the user.

Array is a data structure consisting of collections of elements each identified by atleast one array index or key. 

E.g. If you want to store 5 numbers in an array,

        datatype arrayname [arraysize];

         int a[5];

Step 1 : Import java.util.Scanner Class

Step 2 : Create the object of the Scanner Class

Step 3 : Create and Initialize array

Step 4 : Create variable sum and initialize it to zero

Step 5 : Ask the user to enter the elements by using println method 

Step 6 : Apply for loop where i is equal to 0, i is less than 10 and increment i

Step 7 : Store the user entered elements in the array of i

Step 8 : Apply advanced for loop where num : array

Step 9 : Add sum and num and save its result in the variable sum

Step 10 : Print the Sum of the array elements.

Program :

import java.util.Scanner;
public class SumArray2 
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            int[] array = new int[10];
            int sum = 0;
            System.out.println("Enter the elements:");
            for(int i = 0; i<10; i++)
            {
                array[i] = sc.nextInt();
            }
            for(int num : array)
            {
                sum = sum + num;
            }
                System.out.println("Sum of array elements is:"+sum);
           
        }
    
}

Output :

Enter the elements:
1
2
3
4
5
6
7
8
9
10
Sum of array elements is:55

For Execution of the same do visit my YouTube Channel :





ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages