Efficiently Reading Input For Competitive Programming using Java 810 Sept 2024 | 4 min read As we all know, while solving any CP problems, the very first step is collecting input or reading input. A common mistake we all make is spending too much time on writing code and compile-time as well. In Java, it is recommended to use BufferedReader over Scanner to accept input from the user. Why? It is discussed in one of our previous articles here. (Also, the issues associated with the java.util.Scanner is available) Yet for a better understanding, we will go through both the implementations in this article. Ways of Reading Inputs Using Scanner class Using BufferedReader class Using BufferedReader class with help of streams (More optimized) Now let us discuss ways of reading individually to depth by providing clean java programs and perceiving the output generated from the custom input. Way 1: Simple Scanner Input Reading The java.util.Scanner class provides inbuilt methods to read primitive data from the console along with the lines of text. In the below code snippet let's understand how it is done. The above example illustrates the most common approach used by the majority of programmers while solving competitive programming problems. But what if we can enhance our code a bit to make it faster and reliable? Method 2: Simple BufferedReader Input Reading java.io.BufferedReader class does not provide any method to read primitive data inputs. Java.io.BufferedReader class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of the sequence of characters. Although it throws a checked exception known as IOException. Let us see how to handle that exception and read input from the user. Consider custom input as below as follows: Input: 4 5.6 1 2 3 4 5 Example The above example illustrates another common approach used to read the data while solving competitive programming problems. So is this enough? What if we can enhance it even more? Yes. It is possible. Stay tuned. Method 3: Enhanced way for reading separated data using BufferedReader via Streams In the previous examples, we have seen while reading space-separated data we stored it first in a String array, and then we iterated over elements and then used java typecasting to convert it to the required data type. How about a single line of code making this possible? Yes. Java 8's stream library provides a variety of functions to make it easy and optimized. Consider custom input as below as follows: Input: 34 55 78 43 78 43 22 94 67 96 32 79 6 33 |
Difference Between Java and Core Java Java is a programming language known for its simplicity, object-oriented nature, and platform independence. It consists of three major editions: Java Standard Edition (JSE), Java Enterprise Edition (JEE), and Java Micro Edition (JME). On the other hand, Core Java specifically...
5 min read
A Java default keyword is an access modifier. If we do not assign any access modifier to variables, methods, constructors, and classes, by default, it is considered as default access modifier. The default keyword is a versatile and powerful tool that plays a crucial role...
10 min read
In this section, we will learn what is a smith number and also create Java programs to check if the given number is smith or not. The smith number program frequently asked in Java coding tests and academics. Smith Number A Smith number is a composite number whose...
4 min read
Java does not support multiple inheritance with classes to avoid the diamond problem, which causes ambiguity when multiple parents provide methods with the same signature. However, with the introduction of default methods in Java 8, multiple inheritance is supported using interfaces. While this enhances flexibility, conflicts...
6 min read
The Optional class from Java is an explicit container object that holds an object of the non-null value which may or may not exist. It was first used in Java 8 to provide a more capable, costly, and more safe approach for handling values that...
4 min read
Vectors are mathematical entities that have both magnitude and direction. In computer programming, vectors are often used to represent quantities that have both magnitude and direction, such as velocity, force, and displacement. Java, being a popular object-oriented programming language, provides built-in support for vector operations through...
8 min read
The switch case in Java has undergone some modification to add some new features in more recent versions of Java. In this tutorial, we are going to discuss the switch case in Java 12. However, before that, let's see an example that shows the implementation of...
3 min read
AbstractSet in Java In Java, the AbstractSet class is a member of the Java Collections Framework and extends the AbstractCollection class. It represents an abstract set, which is an unordered collection of elements with no duplicate values. A set is a data structure that only allows a...
22 min read
In this section, we will learn what is Pig Latin word and how to translate or encode a word into a Pig Latin word. Also, we will implement the logic in a JavaM program to find the Pig Latin string. What is Pig Latin? Pig Latin is a...
3 min read
In Java, when we create an object of the class it occupies some space in the memory (heap). If we do not delete these objects, it remains in the memory and occupies unnecessary space that is not upright from the aspect of programming. To resolve...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India