Programming Articles - Page 2493 of 3363

What is InputMisMatchException in Java how do we handle it?

Maruthi Krishna
Updated on 07-Aug-2019 10:47:22

4K+ Views

From Java 1.5 Scanner class was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions.To read various datatypes from the source using the nextXXX() methods provided by this class namely, nextInt(), nextShort(), nextFloat(), nextLong(), nextBigDecimal(), nextBigInteger(), nextLong(), nextShort(), nextDouble(), nextByte(), nextFloat(), next().Whenever you take inputs from the user using a Scanner class. If the inputs passed doesn’t match the method or an InputMisMatchException is thrown. For example, if you reading an integer data using the nextInt() method and the value ... Read More

Importance of isDaemon() method in Java?

raja
Updated on 23-Nov-2023 10:29:10

596 Views

A daemon thread is a low-priority thread in java which runs in the background and mostly created by JVM for performing background tasks like Garbage Collection(GC). If no user thread is running then JVM can exit even if daemon threads are running. The only purpose of a daemon thread is to serve user threads. The isDaemon() method can be used to determine the thread is daemon thread or not. Syntax Public boolean isDaemon() Example class SampleThread implements Runnable { public void run() { if(Thread.currentThread().isDaemon()) System.out.println(Thread.currentThread().getName()+" is daemon thread"); ... Read More

Can we define a package after the import statement in Java?

Vivek Verma
Updated on 28-Aug-2025 16:13:17

690 Views

No, we cannot define a package after the import statement in Java. The compiler will throw an error if we try to insert a package after the import statement. A package is a group of similar types of classes, interfaces, and sub-packages. To create a class inside a package, declare the package name in the first statement in our program. Important! If you use the package statement in an online compiler, you may still get an error even if it is defined correctly. This is because many online compilers do not support custom package structures. Example In the following example, ... Read More

What changes has been introduced in JDK7 related to Exception handling in Java?

Maruthi Krishna
Updated on 07-Aug-2019 09:17:57

119 Views

Since Java 7 try-with resources was introduced. In this we declare one or more resources in the try block and these will be closed automatically after the use. (at the end of the try block)The resources we declare in the try block should extend the java.lang.AutoCloseable class.ExampleFollowing program demonstrates the try-with-resources in Java.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopying {    public static void main(String[] args) {       try(FileInputStream inS = new FileInputStream(new File("E:\Test\sample.txt"));       FileOutputStream outS = new FileOutputStream(new File("E:\Test\duplicate.txt"))){          byte[] buffer = new byte[1024];     ... Read More

Are the instances of Exception checked or unchecked exceptions in java?

Maruthi Krishna
Updated on 07-Aug-2019 09:16:03

629 Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.There are two types of exceptions in Java.Unchecked Exception − An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.Checked Exception − A checked exception is an exception that occurs at ... Read More

Why we can't initialize static final variable in try/catch block in java?

Maruthi Krishna
Updated on 07-Aug-2019 09:11:01

1K+ Views

In Java you can declare three types of variables namely, instance variables, static variables and, local variables.Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.Class (static) variables − Class variables are variables declared within a class, outside any method, with the static keyword.Static methods in try-blockIn the same way, static variables belong to the class and can be accessed anywhere within the class, which contradicts with the definition of the local variable. Therefore, declaring ... Read More

How can we decide that custom exception should be checked or unchecked in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:02:42

6K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.User defined exceptionsYou can create your own exceptions in Java and they are known as user defined exceptions or custom exceptions.To create a user defined exception extend one of the above mentioned classes. To display the message override the toString() method or, call the superclass parameterized constructor by passing the message in String format.MyException(String msg){    super(msg); } Or, public String toString(){    return ... Read More

float() in Python

Pradeep Elance
Updated on 07-Aug-2019 08:55:11

232 Views

Float method is part of python standard library which converts a number or a string containing numbers to a float data type. There are following rules when a string is considered to be valid for converting it to a float.The string must have only numbers in it.Mathematical operators between the numbers can also be used.The string can represent NaN or infThe white spaces at the beginning and end are always ignored.ExampleThe below program indicates how different values are returned when float function is applied.n = 89 print(type(n)) f = float(n) print(type(f)) print("input", 7, " with float function becomes ", float(7)) ... Read More

Finding Mean, Median, Mode in Python without Libraries

Pradeep Elance
Updated on 07-Aug-2019 08:51:42

11K+ Views

Mean, Median and Mode are very frequently used statistical functions in data analysis. Though there are some python libraries.Finding MeanMean of a list of numbers is also called average of the numbers. It is found by taking the sum of all the numbers and dividing it with the count of numbers. In the below example we apply the sum() function to get the sum of the numbers and th elen() function to get the count of numbers.Examplenum_list = [21, 11, 19, 3, 11, 5] # FInd sum of the numbers num_sum = sum(num_list) #divide the sum with length of the ... Read More

Find the k most frequent words from data set in Python

Pradeep Elance
Updated on 07-Aug-2019 08:48:10

1K+ Views

If there is a need to find 10 most frequent words in a data set, python can help us find it using the collections module. The collections module has a counter class which gives the count of the words after we supply a list of words to it. We also use the most_common method to find out the number of such words as needed by the program input.ExamplesIn the below example we take a paragraph, and then first create a list of words applying split(). We will then apply the counter() to find the count of all the words. Finally ... Read More

Advertisements