Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 16 of 50

How to overwrite a line in a .txt file using Java?

Maruthi Krishna
Maruthi Krishna
Updated on 12-May-2025 20K+ Views

The given task is to overwrite a line in a .txt file with new content. Assume we have a .txt file with the following content - Line 1: Hello World Line 2: This is a test file. Line 3: This line will be overwritten. Line 4: Goodbye! We want to overwrite line 3 with "This line has been changed". Following will be the resultant content - line 1: Hello World line 2: This is a test file. line 3: This line has been changed. line 4: Goodbye! Overwriting a Line in a ".txt" File There are no direct methods ...

Read More

What happens when we try to add a duplicate key into a HashMap object in java?

Maruthi Krishna
Maruthi Krishna
Updated on 08-May-2025 4K+ Views

No, HashMap does not allow duplicate keys. The HashMap is a class that implements the Map interface. It is based on the Hash table. It is used to store key-value pairs. It allows null keys and values, but it does not allow duplicate keys. You can store key-value pairs in the HashMap object. Once you do so, you can retrieve the values of the respective keys, but the values we use for keys should be unique. Let's understand what will happen if we try to add a duplicate key into a HashMap.Addng a duplicate key to a HashMapThe put() method ...

Read More

Reading data from keyboard using console class in Java

Maruthi Krishna
Maruthi Krishna
Updated on 08-May-2025 1K+ Views

The Console class is part of the Java.io package and is used to read input from the keyboard or user and write output to the console. Unlike Scanner, the Console class provides methods to read text and passwords. If you read a password using the Console class, it will not be displayed to the user (without showing the typed characters). To use the Console class, we need its object, and the Console object is obtained by calling System.console() as shown below - Console console = System.console(); Note: If you try to execute the program in a non-interactive environment like an IDE, it doesn’t work. It ...

Read More

Why do we get ClassNotFoundException when the class exists in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 24-Apr-2025 4K+ Views

Whenever we try to load a class, ClassNotFoundException occurs when the application tries to load a class at runtime using methods like Class.forName() or ClassLoader.loadClass(), but the JVM cannot locate the class in the classpath. The following are the reasons of ClassNotFoundException occurence and they are - Incorrect Class Name Incorrect Package Structure ...

Read More

Can we change method signature in overriding in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 24-Apr-2025 2K+ Views

No, you cannot change the method signature while overriding. Changing the method signature becomes method overloading, not overriding. Now, let's understand why - Method Signature In Java, a method signature consists of - Method name: It is used to call the method. Parameter list: It includes the number, type, and order of parameters. Note: The method signature does not include return type, ...

Read More

Difference between constants and final variables in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 24-Apr-2025 13K+ Views

In Java, both constants and final variables are used to define variables that cannot be changed after initialization. But they have some differences. In this article, we will learn how they are different. Final Variable A final variable in Java means you cannot reassign it after it has been initialized. Whether it is a local variable, instance variable, or static variable, once it's assigned, the value cannot be changed. If you try to do so, a compile-time error will be generated. public class FinalExample { public static void main(String args[]) { ...

Read More

How to Sort a String in Java alphabetically in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 91K+ Views

Sorting a string alphabetically means rearranging its characters in order, from A to Z. For example, the string "java" becomes "aajv" after sorting. Different ways to sort a String Alphabetically There are two ways to do this in Java using different approaches - Using toCharArray() and Arrays.sort() Method Sorting the array manually Using toCharArray() and Arrays.sort() Method The toCharArray() method of this ...

Read More

In how many ways you can retrieve the elements of a collection in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 3K+ Views

In Java, the collections framework provides various classes like ArrayList, HashSet, and LinkedList to store groups of elements. But once data is stored, how do you access or retrieve it? Java provides multiple ways to retrieve elements from collections, depending on whether you're reading values, modifying them during traversal, or iterating forward or backward. You can retrieve the elements of a collection in four ways- Using For-Loop Using For-Each Loop ...

Read More

How many ways are there to convert an Array to ArrayList in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 451 Views

When working with collections in Java, you might need to convert an array to an ArrayList. This is useful because ArrayList offers more flexibility, such as dynamic sizing and built-in methods for adding, removing, and searching elements. Need for Converting an Array to an ArrayList Arrays in Java have a fixed size and provide limited functionality. ArrayList, on the other hand, is part of the Java Collections Framework and provides methods to modify data dynamically. It is useful when - Need to use dynamic Collections. ...

Read More

Remove all non-alphabetical characters of a String in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 2K+ Views

When you are working with strings in Java, you may want to keep only the alphabetic characters (A-Z, a-z) and remove everything else, like numbers, punctuation, and symbols. Java gives you multiple ways to do this for different cases or scenarios. Removing non-alphabetical characters from a string There are different methods, and they are - Using split() method Using replaceAll() method ...

Read More
Showing 151–160 of 500 articles
« Prev 1 14 15 16 17 18 50 Next »
Advertisements