karthikeya Boyini

karthikeya Boyini

1,421 Articles Published

Articles by karthikeya Boyini

Page 45 of 143

Precision on a number format in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

You can include a precision specifier to the following format specifiers −%f %e %g %sOn floating point, the number of decimal places is known.Let’s say we declared a Formatter object −Formatter f1 = new Formatter();Now, we want 3 decimal places. For that, use 1.3f −f1.format("%1.3f", 29292929.98765432);The above will return the number with 3 decimal places −29292929.988The following is the final example −Exampleimport java.util.Formatter; public class Demo {     public static void main(String args[]) {        Formatter f1,  f2,  f3;        f1 = new Formatter();        f1.format("%1.3f",  29292929.98765432);        System.out.println(f1);        f2 = new Formatter();        f2.format("%1.7f",  29292929.98765432);        System.out.println(f2);     ...

Read More

Display Month of Year using Java Calendar

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 4K+ Views

For using Calendar class, import the following package.import java.util.Calendar;Using the Calendar class, create an object.Calendar calendar = Calendar.getInstance();Now, create a string array of the month names.String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };Display the month name.month[calendar.get(Calendar.MONTH)]The following is an example.Exampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };       System.out.println("Current Month = " + month[calendar.get(Calendar.MONTH)]);   ...

Read More

Get the asymmetric difference of two sets in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 768 Views

Use removeAll() method to get the asymmetric difference of two sets.First set −HashSet set1 = new HashSet (); set1.add("Mat"); set1.add("Sat"); set1.add("Cat");Second set −HashSet set2 = new HashSet (); set2.add("Mat");To get the asymmetric difference −set1.removeAll(set2);The following is an example that displays how to get the asymmetric difference between two sets −Exampleimport java.util.*; public class Demo {    public static void main(String args[]) {       HashSet set1 = new HashSet ();       HashSet set2 = new HashSet ();       set1.add("Mat");       set1.add("Sat");       set1.add("Cat");       System.out.println("Set1 ...

Read More

Display hour in h (1-12 in AM/PM) format in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 303 Views

The h format in Java Date is like 1-12 hour in AM/ PM. Use SimpleDateFormat("h") to get the same format;// displaying hour in h format SimpleDateFormat simpleformat = new SimpleDateFormat("h"); String strHour = simpleformat.format(new Date()); System.out.println("Hour in h format = "+strHour);Above, we have used the SimpleDateFormat class, therefore the following package is imported −import java.text.SimpleDateFormat;The following is an example −Exampleimport java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { // displaying current date and time ...

Read More

C# Enum ToString() Method

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 3K+ Views

The ToString() method converts the value of this instance to its equivalent string representation.Firstly, set an enum.enum Vehicle { Car, Bus, Truck, Motobike };To convert it to an equivalent string representation, use ToString().Vehicle.Car.ToString("d")Exampleusing System; public class Demo {    enum Vehicle { Car, Bus, Truck, Motobike };    public static void Main() {       Console.WriteLine("Vehicle.Car = {0}", Vehicle.Car.ToString("d"));       Console.WriteLine("Vehicle.Bus = {0}", Vehicle.Bus.ToString("d"));    } }OutputVehicle.Car = 0 Vehicle.Bus = 1

Read More

Java program to check if binary representations of two numbers are anagram

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 209 Views

The binary representations of two numbers are anagrams if they have the same number of 0’a and 1’s. An example of this is given as follows −Number 1 = 3 Binary representation of Number 1 = 0011 Number 2 = 12 Binary representation of Number 2 = 1100The two numbers are anagram.A program that demonstrates this is given as follows −Examplepublic class Example { public static void main (String[] args) { long x = 12, y = 3; if(Long.bitCount(x) == Long.bitCount(y)) ...

Read More

Convert an ArrayList to an Array with zero length Array in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 279 Views

An ArrayList can be converted into an Array using the java.util.ArrayList.toArray() method. This method takes a single parameter i.e. the array of the required type into which the ArrayList elements are stored and it returns an Array that contains all the elements of the ArrayList in the correct order.A program that demonstrates this is given as follows −Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       List aList = new ArrayList();       aList.add("James");       aList.add("Harry");       aList.add("Susan");       aList.add("Emma");       aList.add("Peter"); ...

Read More

What are Add, Remove methods in C# lists?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

The List is a collection in C# and is a generic collection. The add and remove methods are used in C# lists for adding and removing elements.Let us see how to use Add() method in C#.Exampleusing System; using System.Collections.Generic; class Program {    static void Main() {       List sports = new List();       sports.Add("Football");       sports.Add("Tennis");       sports.Add("Soccer");       foreach (string s in sports) {          Console.WriteLine(s);       }    } }OutputFootball Tennis SoccerLet us see how to use Remove() method in C#.Exampleusing System; ...

Read More

Boolean Literals in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 875 Views

The Boolean literals have two values i.e. True and False.The following is an example to display Boolean Literals.Examplepublic class Demo {    public static void main(String[] args) {       System.out.println("Boolean Literals");       boolean one = true;       System.out.println(one);       one = false;       System.out.println(one);    } }OutputBoolean Literals true falseIn the above program, we have declared a boolean value and assigned the boolean literal “true”.boolean one = true;In the same way, we have added another boolean literal “false”.one = false;Both of the above values are displayed, which are the boolean literals.

Read More

Get current time information in Java

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 314 Views

Import the following package for to work with Calendar class in Java, import java.util.Calendar;Create a calendar class now.Calendar cal = Calendar.getInstance();To display entire time information, use the following fields.cal.get(Calendar.HOUR_OF_DAY) cal.get(Calendar.HOUR) cal.get(Calendar.MINUTE) cal.get(Calendar.SECOND) cal.get(Calendar.MILLISECOND)The following is the final example.Exampleimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar cal = Calendar.getInstance();       // current date and time       System.out.println(cal.getTime().toString());       // time information       System.out.println("Hour (24 hour format) : " + cal.get(Calendar.HOUR_OF_DAY));       System.out.println("Hour (12 hour format) : " + cal.get(Calendar.HOUR));       ...

Read More
Showing 441–450 of 1,421 articles
« Prev 1 43 44 45 46 47 143 Next »
Advertisements