Found 33676 Articles for Programming

How to extract multiple integers from a String in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:25

858 Views

Let’s say the following is our string with integer and characters −String str = "(29, 12; 29, ) (45, 67; 78, 80)";Now, to extract integers, we will be using the following pattern −\dWe have set it with Pattern class −Matcher matcher = Pattern.compile("\d+").matcher(str);Example Live Demoimport java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String[] args) {       String str = "(29, 12; 29, ) (45, 67; 78, 80)";       Matcher matcher = Pattern.compile("\d+").matcher(str);       Listlist = new ArrayList();       while(matcher.find()) {          list.add(Integer.parseInt(matcher.group()));       }       System.out.println("Integers = "+list);    } }OutputIntegers = [29, 12, 29, 45, 67, 78, 80]

Java Program to get frequency of words with Lambda Expression

Alshifa Hasnain
Updated on 13-Dec-2024 13:55:29

625 Views

In this article, we will learn how to calculate the frequency of words in a text using Java and Lambda Expressions. By leveraging concise syntax and functional programming techniques, we can efficiently process text data and determine word occurrences. This approach is particularly useful for applications like text analysis and natural language processing. What is Lambda Expression? A lambda expression in Java is a concise way to represent an anonymous function(a function without a name). Introduced in Java 8, it simplifies the implementation of functional interfaces (interfaces with a single abstract method), enabling cleaner and more readable code. Syntax of ... Read More

How to display numbers in scientific notation in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:25

5K+ Views

To display number in scientific notation, create NumberFormat object first −NumberFormat numFormat = newDecimalFormat();Now, let’s say you need to format the minimum value of Integer −int i = Integer.MIN_VALUE; System.out.println(i); numFormat = newDecimalFormat("0.######E0"); System.out.println(numFormat.format(i)); numFormat = newDecimalFormat("0.#####E0"); System.out.println(numFormat.format(i));Above, we have used format() method of the NumberFormat class.Example Live Demoimport java.text.DecimalFormat; import java.text.NumberFormat; public class Demo {    public static void main(String args[]) {       NumberFormat numFormat = new DecimalFormat();       int i = Integer.MIN_VALUE;       System.out.println(i);       numFormat = new DecimalFormat("0.######E0");       System.out.println(numFormat.format(i));       numFormat = new DecimalFormat("0.#####E0");   ... Read More

Set Date value in Java HashMap?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

Create a Calendar instance and Date object −Calendar cal = Calendar.getInstance(); Date date = new Date(); cal.setTime(date);Now, create a HashMap and store Date value −LinkedHashMaphashMap = new LinkedHashMap(); hashMap.put("year", cal.get(Calendar.YEAR)); hashMap.put("month", cal.get(Calendar.MONTH)); hashMap.put("day", cal.get(Calendar.DAY_OF_MONTH));Example Live Demoimport java.util.Calendar; import java.util.Date; import java.util.LinkedHashMap; public class Demo {    public static void main(String[] argv) {       Calendar cal = Calendar.getInstance();       Date date = new Date();       System.out.println("Date = "+date);       cal.setTime(date);       LinkedHashMaphashMap = new LinkedHashMap();       hashMap.put("year", cal.get(Calendar.YEAR));       hashMap.put("month", cal.get(Calendar.MONTH));       hashMap.put("day", cal.get(Calendar.DAY_OF_MONTH));     ... Read More

How to display random numbers less than 20 in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

322 Views

At first, create a Random class object −Random rand = new Random();Now, create a new array −int num; int arr[] = new int[10];Loop through and set the Random nextInt with 20 as parameter since you want random numbers less than 20 −for (int j = 0; j

How to calculate the possibilities of duplication for random number within a range in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

189 Views

To get the duplicate numbers for random numbers in a range, loop through and create two Random class objects −Use nextInt() to get the next number −intrandVal1 = new Random().nextInt(50); intrandVal2 = new Random().nextInt(50);Now, compare both the above numbers −if (randVal1 == randVal2) {    System.out.println("Duplicate number = "+randVal1); }All the above is to be done in a loop −for (int i = 1; i

Java Program to check if a Float is Infinite or Not a Number(NAN)

Samual Sam
Updated on 30-Jul-2019 22:30:25

2K+ Views

To check if a Float is isInfinite, use the isInfinite() method and to check for NAN, use the isNaN() method.Example Live Demopublic class Demo {    public static void main(String[] args) {       float value1 = (float) 1 / 0;       boolean res1 = Float.isInfinite(value1);       System.out.println("Checking for isInfinite? = "+res1);       float value2 = (float) Math.sqrt(9);       boolean res2 = Float.isNaN(value2);       System.out.println("Checking for isNan? = "+res2);    } }OutputChecking for isInfinite? = true Checking for isNan? = false

Java program to get minutes between two time instants

karthikeya Boyini
Updated on 18-Oct-2024 11:58:13

905 Views

In this article, we will calculate the number of minutes between two-time instants using Java. This will be done by using the Instant and Duration classes from the java.time package. We'll create two instances of time, add specific hours and minutes to one of them, and then compute the difference in minutes between the two.Steps to get minutes between two-time instantsFollowing are the steps to get minutes between two-time instants −First, import the necessary classes: Duration, Instant, and ChronoUnit from the java.time package.Create an instance of the current time using Instant.now().Add 5 hours and 10 minutes to the first time instance to ... Read More

Java program to get Milliseconds between two time instants

Samual Sam
Updated on 12-Aug-2024 23:13:17

3K+ Views

In this article, we will demonstrate how to calculate the difference in milliseconds between two Instant objects using the ChronoUnit class. It shows how to work with time-based classes in the java.time package, specifically focusing on creating and manipulating time instants and calculating durations between them. ChronoUnit: A standard set of date and time units lets you manipulate dates, times, and date-times. These units, such as years, months, and days, are designed to work across various calendar systems, even though the specific rules might differ slightly. You can also extend these units by implementing the TemporalUnit interface. Problem Statement Calculate the ... Read More

Java Program to convert java.util.Date to LocalDate

Samual Sam
Updated on 30-Jul-2019 22:30:25

273 Views

At first set the date with java.util.Date −java.util.Date date = new Date();Now, convert the date to LocalDate −Instant instant = Instant.ofEpochMilli(date.getTime()); System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());Example Live Demoimport java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       java.util.Date date = new Date();       System.out.println("Date = "+date);       Instant instant = Instant.ofEpochMilli(date.getTime());       System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());    } }OutputDate = Thu Apr 18 23:51:06 IST 2019 LocalDate = 2019-04-18

Advertisements