Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Java Articles
Page 74 of 450
What is EOFException in Java? How do we handle it?
While reading the contents of a file in certain scenarios the end of the file will be reached in such scenarios a EOFException is thrown.Especially, this exception is thrown while reading data using the Input stream objects. In other scenarios a specific value will be thrown when the end of file reached.Let us consider the DataInputStream class, it provides various methods such as readboolean(), readByte(), readChar() etc.. to read primitive values. While reading data from a file using these methods when the end of file is reached an EOFException is thrown.ExampleFollowing program demonstrates how to handle the EOFException in Java.import ...
Read MoreElaborate the legal operands of the instance of operator in java?
The instanceof operator in Java is used to find whether a reference is an instance of a Type i.e. class or an interface.Examplepublic class InstanceOfExample { public static void main(String args[]) { String str = "hello"; boolean bool = str instanceof String; System.out.println(bool); } }OutputtrueLegal operands of the instanceof operatorThe following are the only legal operands of the instanceof operator −Left operand − It must be a reference representing an object.Right operand − It must be the name of Java class or, interface.Except these two if you use any ...
Read MoreDifferentiate between the prefix and postfix forms of the ++ operator in java?
Java provides two operators namely ++ and --, to increment and decrement values by 1 respectively.There are two variants of these operators −Pre-increment/decrement − This form, increments/decrements the value first, and then performs the specified operation.ExampleIn the following example, the initial value of the variable i is 5. We are printing the incremented value of it using the pre increment operator.Since we are using the pre increment operator, the value of i is incremented then printed.public class ForLoopExample { public static void main(String args[]) { int i = 5; System.out.println(++i); ...
Read MoreCan a for statement loop infinitely in java?
Using loops in programming languages we can execute a set of statements repeatedly. Java provides various loops namely while loop, for loop and the do while loop.The for statement contains an initialization statement, a condition and, an increment or decrement operation.Initializing statement − The initialization determines the starting value of the loop.Condition − The condition in for loop is a statement returning a boolean value. This condition determines the exit value of the loop. It is executed before the statements of the loop.Increment and decrement − Using this the loop will be incremented or decremented to the next value.After initializing ...
Read MoreJava Programs for printing Pyramid Patterns. (of numbers)
Following is a Java program which prints a pyramid of numbers.Examplepublic class PrintingPyramids { public static void main(String args[]){ int n,i,j,k=1; n = 5; for(i = 0; i
Read MoreJava program to print a given pattern. (stars)
Following is a Java program which prints a pyramid of numbers.Examplepublic class PrintingPyramidStars { public static void main(String args[]){ int n,i,j; n = 5; for(i = 0; i
Read MoreJava program to cyclically rotate an array by one.
To rotate the contents of an array cyclically −create an empty variable. (temp)save the last element of the array in it.Now, starting from the nth element of the array, replace the current element with the previous element.Store the element in temp in the 1st position.Exampleimport java.util.Arrays; import java.util.Scanner; public class CyclicallyRotateanArray { 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 ...
Read MoreRecursive program to find an element in an array linearly.
Following is a Java program to find an element in an array linearly.Exampleimport 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
Read MoreProgram to print Sum Triangle of an array.
To generate a sum triangle of a given arrayGet the elements of the array from the user say, myArray[n].Create a two dimensional array say, result[n][n].Store the contents of the given array in the first row from bottom of the 2D array.result[n][i] = myArray[i].Now, from the second row of the 2D array fill elements such that ith element in each row is the sum of ith and (i+1)st elements of the previous row.result[i][j] = result[i+1][j] + result[i+1][j+1];Exampleimport java.util.Scanner; public class SumTriangleOfAnArray { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter the ...
Read MoreProgram for printing array in Pendulum Arrangement.
To arrange elements of the array following pendulum arrangement.Sort the given array, create an empty array to store the result.Store the 0th element in a variable say temp.Store the element at index 1 in the sorted array in (mid+1)st position of the resultant array, and the next element int the (mid-1)st position and the next element in (mid+2)nd element and so on.Exampleimport 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]; ...
Read More