Tutorialspoint

Java8 Programming Examples

Multiple Catch Blocks in Java

import java.util.Arrays;
import java.util.Scanner;
public class MultipleCatchBlocks {
   public static void main(String [] args) {
      Scanner sc = new Scanner(System.in);
      int[] arr = {10, 20, 30, 2, 0, 8};
      System.out.println("Enter 3 integer values one by one: ");
      System.out.println("Array: "+Arrays.toString(arr));
      System.out.println("Choose numerator and denominator(not 0) from this array (enter positions 0 to 5)");
      int a = sc.nextInt();
      int b = sc.nextInt();
      try {
         int result = (arr[a])/(arr[b]);
         System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
      }
      catch(ArrayIndexOutOfBoundsException e) {
         System.out.println("Warning: You have chosen a position which is not in the array");
      }
      catch(ArithmeticException e) {
         System.out.println("Warning: You cannot divide an number with 0");
      }
   }
}

Convert an array to String in Java

import java.util.Arrays;
import java.util.Scanner;
public class ObjectArrayToStringArray {	
   public static void main(String args[]){	   
      Scanner sc = new Scanner(System.in);
      
      //Integer array to String
      System.out.println("Enter 5 integer values: ");
      int intArray[] = new int[5]; 
      for(int i=0; i<5; i++){    	  
         intArray[i] = sc.nextInt();
      }	 	            
      System.out.println("Contents of the integer array: "+Arrays.toString(intArray));
   
      //float array to String
      System.out.println("Enter 5 float values: ");
      float floatArray[] = new float[5]; 
      for(int i=0; i<5; i++){    	  
         floatArray[i] = sc.nextFloat();
      }	 	            
      System.out.println("Contents of the float array: "+Arrays.toString(floatArray));
   
      //double array to String
      System.out.println("Enter 5 double values: ");
      double doubleArray[] = new double[5]; 
      for(int i=0; i<5; i++){    	  
         doubleArray[i] = sc.nextDouble();
      }	 	            
      System.out.println("Contents of the double array: "+Arrays.toString(doubleArray));
   
      //byte array to String
      System.out.println("Enter 5 byte values: ");
      byte byteArray[] = new byte[5]; 
      for(int i=0; i<5; i++){    	  
         byteArray[i] = sc.nextByte();
      }	 	            
      System.out.println("Contents of the byte array: "+Arrays.toString(byteArray));
   
      //char array to String
      System.out.println("Enter 5 character values: ");
      char charArray[] = new char[5]; 
      for(int i=0; i<5; i++){    	  
         charArray[i] = sc.next().toCharArray()[0];
      }	 	            
      System.out.println("Contents of the char array: "+Arrays.toString(charArray));
   
   
      //object array to String
      System.out.println("Enter 5 String values: ");
      Object objArray[] = new Object[5]; 
      for(int i=0; i<5; i++){    	  
    	  objArray[i] = sc.next();
      }	 	            
      System.out.println("Contents of the object array: "+Arrays.toString(objArray));
      
   
   }
}

Print a given matrix in Spiral Form in Java

public class PrintMatrixInSpiralForm {  
 public static void main(String args[]){	 
  int a[][]={{1,2,3},{4,5,6},{7,8,9}}; 
  int w=0;
  int x=a.length-1;
   
  int y=0;
  int z = a[0].length-1;
   
  while(w <= x && y <= z){
   
   for (int i = w; i <= z; i++) {
    System.out.print(a[w][i] + " ");
   }
 
   for (int i = w+1; i <= x; i++) {
    System.out.print(a[i][z] + " ");
   }
    
   if(w+1 <= x){
    for (int i = z-1; i >= y; i--) {
     System.out.print(a[x][i] + " ");
    }
   }
   if(y+1 <= z){
    for (int i = x-1; i > w; i--) {
     System.out.print(a[i][y] + " ");
    }
   }
    
   w++;
   x--;
   y++;
   z--;
  }
 }
}

Compile and Execute Java8 Online

import java.util.Scanner;

public class SearchingRecursively {
	
   public static boolean searchArray(int[] myArray, int element, int size){
      if (size == 0){
         return false;
      }
      if (myArray[size-1] == element){
         return true;
      }
      return searchArray(myArray, element, size-1);
   }

   public static void main(String args[]){	   
      System.out.println("Enter the required size of the array: ");     
      Scanner s = new Scanner(System.in);
      int size = s.nextInt();
      int myArray[] = new int [size]; 

      System.out.println("Enter the elements of the array one by one ");
      for(int i=0; i<size; i++){
         myArray[i] = s.nextInt();
      }	 
      
      System.out.println("Enter the element to be searched: ");     
      int target = s.nextInt();      
	  boolean bool = searchArray(myArray, target ,size);

      if(bool){
         System.out.println("Element found");
      }else{
    	 System.out.println("Element not found");    	  
      }
   }
}

Compile and Execute Java8 Online

public class SubtractingMatrices{  
   public static void main(String args[]){
      int a[][]={{1,2,3},{4,5,6},{7,8,9}};    
      int b[][]={{1,1,1},{1,1,1},{1,1,1}};    
      int c[][]=new int[3][3]; 
    
      for(int i=0;i<3;i++){    
    	  for(int j=0;j<3;j++){    
    		  c[i][j] = a[i][j]-b[i][j];
    		  System.out.print(c[i][j]+" ");
    	  } 
    	  System.out.println(); 
      }    
   }
} 

print Sum Triangle of an array in Java

import java.util.Scanner;
public class SumTriangleOfAnArray {
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);   
      System.out.println("Enter the required size of the array :");
      int size = sc.nextInt();
      int [] myArray = new int[size];
      System.out.println("Enter elements of the array :");
      for(int i = 0; i< size; i++){
         myArray[i] = sc.nextInt(); 
      }
      
      int[][] result = new int [size][size];
      
      for(int i = 0; i<size; i++){
    	  result[size-1][i] = myArray[i];
      }
      
      for (int i=size-2; i >=0; i--){
         for (int j = 0; j <= i; j++){
            result[i][j] = result[i+1][j] + result[i+1][j+1];
          }
      }
      for (int i=0; i<result.length; i++){
         for (int j=0; j<size; j++){     
        	if(result[i][j]!= 0){
        	   System.out.print(result[i][j]+" ");        	
            } 
         }
         System.out.println();
      }
   }
}

Compile and Execute Java8 Online

public class HelloWorld{

     public static void main(String []args){
        System.outpublic class PrintMatrixInSpiralForm {  
 public static void main(String args[]){	 
  int a[][]={{1,2,3},{4,5,6},{7,8,9}}; 
  int w=0;
  int x=a.length-1;
   
  int y=0;
  int z = a[0].length-1;
   
  while(w <= x && y <= z){
   
   for (int i = w; i <= z; i++) {
    System.out.print(a[w][i] + " ");
   }
 
   for (int i = w+1; i <= x; i++) {
    System.out.print(a[i][z] + " ");
   }
    
   if(w+1 <= x){
    for (int i = z-1; i >;= y; i--) {
     System.out.print(a[x][i] + " ");
    }
   }
   if(y+1 <= z){
    for (int i = x-1; i > w; i--) {
     System.out.print(a[i][y] + " ");
    }
   }
    
   w++;
   x--;
   y++;
   z--;
  }
 }
}.println("Hello World");
     }
}

Converting Alternate characters of a string to Upper Case in Java

import java.util.Scanner;

public class UpperCase {
   public static void main(String[] args) {      
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string :");
      String str = sc.nextLine();
      str = str.toLowerCase();      
      char[] ch = str.toCharArray();      
      for(int i=0; i<ch.length; i=i+2){
    	 ch[i] = Character.toUpperCase(ch[i]);
      }
      System.out.println(new String(ch));     
   } 
}

Print a matrix in Diagonal Pattern in Java

public class DiagonalMatrix {
   public static void main(String args[]){
      int a[][]={{1,2,3},{4,5,6},{7,8,9}};
      int rows = a.length;
      int columns = a[0].length;
      
      for (int i = 0; i < rows; i++) {
         for (int r = i, c = 0; r >= 0 && c < columns; r--, c++){
    	    System.out.print(a[r][c] + " "); 
    	 }
         System.out.println();
      }
      
      for (int i = 1; i < columns; i++) {
         for (int r = rows-1, c = i; r >= 0 && c < columns; r--, c++) {
            System.out.print(a[r][c] + " "); 
         }
         System.out.println();
      }
         
   }
}

Printing array in Pendulum Arrangement in Java

import java.util.Arrays;
import java.util.Scanner;

public class ArrayinPendulumArrangement {	
   public static int[] swap(int origPos, int newPos, int[] array){
      origPos = 1;
      newPos = 4;
      int temp = array[origPos];
      array[origPos] = array[newPos];      
      array[newPos] = temp;
      System.out.println(Arrays.toString(array));
      return array;
   }
   public static void main(String args[]){
      Scanner sc = new Scanner(System.in);   
      System.out.println("Enter the required size of the array (odd) :");
      int size = sc.nextInt();
      int [] myArray = new int[size];
      System.out.println("Enter elements of the array :");
      for(int i = 0; i< size; i++){
         myArray[i] = sc.nextInt(); 
      }
      //Sort the given array
      Arrays.sort(myArray);
      System.out.println("Contents of the Array"+Arrays.toString(myArray));
      
      int temp = myArray[0];
      int mid = (size-1)/2;
      int k =1;
      int[] result = new int[size];
      
      for(int i = 1; i<=mid; i++){
          result[mid+i] = myArray[k++];
          result[mid-i] = myArray[k++];
      }      
      result[mid] = temp;      
      System.out.println("Pendulum arrangement "+Arrays.toString(result));
   }
}

1 2 3 4 5 6 7 ... 516 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.