Java Examples

In this tutorial, I’m going to guide you through the process of zipping and unzipping files in Java. This is a common task in many programming and data management scenarios, and it’s a skill that can be incredibly useful in a variety of contexts. Throughout this tutorial, you’ll learn how to zip a single file,…

Read More Zip and Unzip Files in Java

When working with Java programming, it is often essential to determine whether a given string represents a numeric value. This task is crucial for a variety of scenarios, such as validating user input, performing calculations, or processing data. By accurately identifying numeric strings, you can ensure the correctness and integrity of your application’s logic. The…

Read More Java: Check if String is Numeric

Accessing elements from an ArrayList is a fundamental operation in Java programming. ArrayLists are commonly used to store and manage collections of objects dynamically. Being able to retrieve elements enables you to work with the data stored within the ArrayList effectively. Whether you need to display elements, perform calculations, or manipulate the data in any…

Read More How to Get an Element from an ArrayList in Java?

Finding the maximum and minimum values in an array is a common requirement in various programming scenarios. It allows you to extract key insights from your data, such as identifying the highest and lowest values, or determining the range of values present. This knowledge can be utilized in numerous applications, including statistical analysis, data processing,…

Read More Finding Max and Min Values in a Java Array

class PrintArrayElementsExample { public static void main(String[] args) { int[] arr1 = {1, 7, 9, 5, 2, 8, 3}; String[] arr2 = {“Megan”, “Tom”, “Melissa”, “John”, “Steve”}; // print elements of the arr1 System.out.println(Arrays.toString(arr1)); // print elements of the arr2 System.out.println(Arrays.toString(arr2)); } } Output: [1, 7, 9, 5, 2, 8, 3] [Megan, Tom, Melissa, John,…

Read More Print Array Elements in Java

public class SplitString { public static void main(String[] args) { String names = “Tom,Steve,John,Megan,Melissa”; // split string String[] arr = names.split(“,”); // print the size of the array System.out.println(arr.length); // print the elements Stream.of(arr).forEach(System.out::println); } } Output: 5 Tom Steve John Megan Melissa   If you need a list, instead of an array, use the…

Read More Split a comma-separated String in Java

class CheckIfArrayContainsValue { public static void main(String[] args) { String[] programmingLanguages = {“Python”, “Kotlin”, “Ruby”, “JavaScript”, “C#”, “Java”, “Flutter”}; for (String lang : programmingLanguages) { if (lang.equals(“Java”)) { System.out.println(“It does contain!”); break; // value found, exit the loop } } } } Output: It does contain!   Here, we are iterating over the array, and…

Read More Check if Array Contains a Value in Java

Output: Some String   Here, we put the creation of the Scanner object into a try-with-resources because we want to close the Scanner after it reads all the data. We passed the “\\A” to tell the Scanner to read the entire input stream. import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; class ConvertInputStreamToString { /** *…

Read More Convert InputStream to a String in Java