Found 33676 Articles for Programming

How to convert a sub class variable into a super class type in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:52:37

2K+ Views

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{}The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Converting a sub class variable into a super class typeYou can directly assign a sub ... Read More

How to create a single data frame from data frames stored in a list with row names in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:36:59

179 Views

If we have multiples data frames of same size stored in a list and we believe that these data frames have similar characteristic then we can create a single data frame. This can be done with the help of do.call. For example, if we have a list defined with the name List containing data frames with equal number of rows with their names then a single data frame can be created do.call(rbind,unname(List)).Example Live Demodf1

Explain the usage of the valueOf() method of the String class in Java

Maruthi Krishna
Updated on 08-Feb-2021 11:36:43

196 Views

The String class of the java.lang package represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant, their values cannot be changed after they are created.The valueOf() method of the String class accepts a char or, char array or, double or, float or, int or, long or an object as a parameter and returns its String representation.ExampleLive Demoimport java.util.Scanner; public class ConversionOfDouble {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a double value:");       ... Read More

How to convert a double value into a Java String using append method?

Maruthi Krishna
Updated on 08-Feb-2021 11:36:26

270 Views

The append method of the StringBuilder or StringBuffer objects accept a boolean or, char or, char array or, double or, float or, int or, long or, String value as parameter and adds it to the current object.You can append the required double value to the method and retrieve a String from obtained StringBuffer (or, StringBuilder) objects.ExampleLive Demoimport java.util.Scanner; public class ConversionOfDouble {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a double value:");       double d = sc.nextDouble();       StringBuffer sb = new StringBuffer();       sb.append(d); ... Read More

How to iterate the values of an enum using a for loop in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:26:53

440 Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntax −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }You can retrieve the contents of an enum using the values() method. This method returns an array containing all the values. Once you obtain the array you can iterate it using the for loop.Examplepublic class IterateEnum{    public static void main(String args[]) {       Days days[] = Days.values();       System.out.println("Contents of the enum are: ");             //Iterating enum using the for loop ... Read More

Can we define an enum inside a method in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:35:52

2K+ Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntax −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }We can an enumeration inside a class. But, we cannot define an enum inside a method. If you try to do so it generates a compile time error saying “enum types must not be local”.Examplepublic class EnumExample{    public void sample() {       enum Vehicles {          Activa125, Activa5G, Access125, Vespa, TVSJupiter;       }    } }ErrorEnumExample.java:3: error: enum types must not be local ... Read More

Can we define an enum inside a class in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:34:06

17K+ Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntaxenum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.ExampleLive Demopublic class EnumerationExample {    enum Enum {       Mango, Banana, Orange, Grapes, Thursday, Apple    }    public static void main(String args[]) {       Enum constants[] = Enum.values();       System.out.println("Value of constants: ");         for(Enum d: constants) {         ... Read More

How to search a directory with file extensions in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:21:27

1K+ Views

Following example prints the files in a directory based on the extensions −Exampleimport java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) throws IOException {       Stream path = Files.walk(Paths.get("D:\ExampleDirectory"));       System.out.println("List of PDF files:");       path = path.filter(var -> var.toString().endsWith(".pdf"));       path.forEach(System.out::println);             path = Files.walk(Paths.get("D:\ExampleDirectory"));       System.out.println("List of JPG files:");       path = path.filter(var -> var.toString().endsWith(".jpg"));       path.forEach(System.out::println);           path = Files.walk(Paths.get("D:\ExampleDirectory"));       ... Read More

How to list the hidden files in a directory in Java?

Maruthi Krishna
Updated on 08-Feb-2021 11:21:14

866 Views

The ListFiles() method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.The File Filter interface is filter for the path names you can pass this as a parameter to the listFiles() method. This method filters the file names passed on the passed filter.To get the hidden directories in a folder, implement a FileFilter which accepts only hidden directories, and pass it as a parameter to the listFiles() method.Exampleimport java.io.File; import java.io.FileFilter; import java.io.IOException; public class Test{    public static void main(String args[]) throws IOException {   ... Read More

How to handle IllegalArgumentException inside an if using Java

Maruthi Krishna
Updated on 08-Feb-2021 11:30:06

592 Views

While you use the methods that causes IllegalArgumentException, since you know the legal arguments of them, you can restrict/validate the arguments using if-condition before-hand and avoid the exception.We can restrict the argument value of a method using the if statement. For example, if a method accepts values of certain range, you can verify the range of the argument using the if statement before executing the method.ExampleFollowing example handles the IllegalArgumentException caused by the setPriority() method using the if statement.Live Demoimport java.util.Scanner; public class IllegalArgumentExample {    public static void main(String args[]) {       Thread thread = new Thread(); ... Read More

Advertisements