Found 477 Questions for Java8

Reading a Text file in java

George John
Updated on 25-Jun-2020 12:09:36
Java provides Reader classes to read data from various sources. You can read the contents of a file using the BufferedReader class.Programimport java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedReaderExample {    public static void main(String[] args) {       try (BufferedReader br = new BufferedReader(new FileReader("myFile"))) {          String sCurrentLine;                    while ((sCurrentLine = br.readLine()) != null) {             System.out.println(sCurrentLine);          }       } catch (IOException e) {          e.printStackTrace();       }    } }OutputHi welcome to Tutorialspoint

File Permissions in java

Ankith Reddy
Updated on 25-Jun-2020 12:08:19
The Java.io.FilePermission class represents access to a file or directory. It consists of a pathname and a set of actions valid for that pathname. Following are the important points about File Permission −The actions to be granted are passed to the constructor in a string containing a list of one or more comma-separated keywords. The possible keywords are "read", "write", "execute", and "delete".The code can always read a file from the same directory it's in (or a subdirectory of that directory); it does not need explicit permission to do so.ProgramThe java.io.FileOutputStream implies(Permission p) method tests if this FilePermission object "implies" ... Read More

Check if a large number is divisible by 3 or not in java

Arjun Thakur
Updated on 25-Jun-2020 12:05:58
If the sum of the digits of a number is divisible by 3, then the number is divisible by 3.Some examples of numbers divisible by 3 are as follows.The number 85203 is divisible by 3 because the sum of its digits (8 + 5 + 2 + 0 + 3 = 18) is divisible by 3.The number 79154 is not divisible by 3 because the sum of its digits (7 + 9 + 1 + 5 + 4 = 26) is not divisible by 3.Programimport java.util.Scanner; public class DivisibleBy3 {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number :");       String num = sc.nextLine();       int digitSum = 0;             for(int i = 0; i

Check if a large number is divisible by 11 or not in java

Chandu yadav
Updated on 25-Jun-2020 12:03:01
A number is divisible by 11 if the difference between the sum of its alternative digits is divisible by 11.i.e. if (sum of odd digits) – ( sum of even digits) is 0 or divisible by 11 then the given number is divisible by 11.Programimport java.util.Scanner; public class DivisibleBy11 {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number :");       String num = sc.nextLine();       int digitSumEve = 0;       int digitSumOdd = 0;            for(int i = 0; i

To check divisibility of any large number by 9 in java

Arjun Thakur
Updated on 25-Jun-2020 12:01:29
If the sum of the digits of a number is divisible by 9, then the number is divisible by 9.Some examples of numbers divisible by 9 are as follows. The number 51984 is divisible by 9 because the sum of its digits (5+ 1 + 9 + 8 + 4 = 27) is divisible by 9.The number 91403 is not divisible by 9 because the sum of its digits (9 + 1 + 4 + 0 + 3 = 17) is not divisible by 9.Programimport java.util.Scanner; public class DivisibleBy9 {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number :");       String num = sc.nextLine();       int digitSum = 0;       for(int i = 0; i

GCD of an array of numbers in java

Ankith Reddy
Updated on 25-Jun-2020 12:00:32
ProgramFollowing is the example to calculate the GCD of the numbers of an array.Live Demopublic class GCDOfArrayofNumbers{    public static int gcd(int a,int b){       int res = 0;       while (b > 0){          int temp = b;          b = a % b;          a = temp;          res = a;       }       return res;    }    public static void main(String arg[]){       int[] myArray = {3, 6, 8};       int result = gcd(myArray[0],myArray[1]);       for(int i = 2; i < myArray.length; i++){          result = gcd(result, myArray[i]);       }       System.out.println("Gcd of n numbers is: "+result);    } }OutputGCD of n numbers is: 1

LCM of an array of numbers in Java

George John
Updated on 25-Jun-2020 11:59:39
L.C.M. or Least Common Multiple of two values, is the smallest positive value which the multiple of both values.For example multiples of 3 and 4 are:3 → 3, 6, 9, 12, 15 ...4 → 4, 8, 12, 16, 20 ...The smallest multiple of both is 12, hence the LCM of 3 and 4 is 12.ProgramFollowing example computes the LCM of array of numbers.Live Demopublic class LCMofArrayOfNumbers {    public static void main(String args[]) {       int[] myArray = {25, 50, 125, 625};       int min, max, x, lcm = 0;             for(int i = 0; i

GCD and LCM of two numbers in Java

Chandu yadav
Updated on 25-Jun-2020 11:57:26
Following is an example which computes find LCM and GCD of two given numbers.Programimport java.util.Scanner; public class LCM_GCD {    public static void lcm(int a, int b){       int max, step, lcm = 0;       if(a > b){          max = step = a;       } else{          max = step = b;       }       while(a!= 0) {          if(max%a == 0 && max%b == 0) {             lcm = max;             break;          }          max += step;       }       System.out.println("LCM of given numbers is :: "+lcm);    }    public static void gcd(int a,int b){       int i, hcf = 0;          for(i = 1; i

Java Program to Convert a Stack Trace to a String

karthikeya Boyini
Updated on 13-Mar-2020 13:01:05
The Java.io.StringWriter class is a character stream that collects its output in a string buffer, which can then be used to construct a string. Closing a StringWriter has no effect.The Java.io.PrintWriter class prints formatted representations of objects to a text-output stream.Using these two classes you can convert a Stack Trace to a String.ExampleLive Demoimport java.io.PrintWriter; import java.io.StringWriter; public class StackTraceToString {    public static void main(String args[]) {       try {          int a[] = new int[2];          System.out.println("Access element three :" + a[3]);       } catch (ArrayIndexOutOfBoundsException e) ... Read More

Java Program to Check if a String is Numeric

Chandu yadav
Updated on 13-Mar-2020 13:01:54
ExampleYou can check if a given string is Numeric as shown in the following program.Live Demoimport java.util.Scanner; public class StringNumeric {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string ::");       String str = sc.next();       boolean number = str.matches("-?\d+(\.\d+)?");       if(number) {          System.out.println("Given string is a number");       } else {          System.out.println("Given string is not a number");       }    } }OutputEnter a string :: 4245 Given string is a number
Advertisements