Found 9326 Articles for Object Oriented Programming

Get the system properties from RuntimeMXBean in Java

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

190 Views

RuntimeMXBean in the management interface for the runtime system of the Java virtual machine.RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean();To get the system properties, use the getSystemProperties() method −System.out.println("System properties from RuntimeMXBean: "+runtimeMX.getSystemProperties());The following is an example −Example Live Demoimport java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; import java.util.Date; public class Demo { public static void main(String args[]) throws Exception { RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean(); System.out.println("System properties from RuntimeMXBean: "+runtimeMX.getSystemProperties()); } }OutputSystem properties from RuntimeMXBean: {awt.toolkit=sun.awt.X11.XToolkit, file.encoding.pkg=sun.io, java.specification.version=1.8, sun.cpu.isalist=, sun.jnu.encoding=UTF-8, java.class.path=/home/cg/root/GNUstep/Library/Libraries/Java:/usr/GNUstep/Local/Library/Libraries/Java:/usr/G NUstep/System/Library/Libraries/Java::/usr/share/java/mysql-connector- java.jar:.:/var/www/html/lib:/var/www/html/lib/dom4j-1.6.jar:/var/www/html/lib/guava- 18.0.jar:/var/www/html/lib/jackson-all.jar:/var/www/html/lib/jaxen- 1.1.4.jar:/var/www/html/lib/jcommon.jar:/var/www/html/lib/jdom2- 2.0.5.jar:/var/www/html/lib/jfreechart.jar:/var/www/html/lib/junit-4.12.jar:/var/www/html/lib/spymemcached- 2.10.3.jar:/var/www/html/lib/stax-1.2.0.jar:/var/www/html/lib/xstream-1.4.7.jar:/var/www/html/lib/gson- 2.3.1.jar:/var/www/html/lib/hamcrest-core-1.3.jar, java.vm.vendor=Oracle Corporation, ... Read More

Displaying at most 10 characters in a string in Java

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

366 Views

To display at most 10 characters in a string, work with the Formatter class.Import the following package for Formatter class in Java −import java.util.Formatter;Create a new Formatter object −Formatter f = new Formatter();Let us now display at most 10 characters in a string −f = new Formatter(); System.out.println("Displaying at most 10 characters: "+f.format("%.10s", "This is demo text!"));The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); String str = "This is demo ... Read More

Java Program to format to 2 decimal places in a 10-character field

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

134 Views

To format, use the Formatter class. Import the following package to work with the Formatter class in Java −import java.util.Formatter;Create a Formatter object and format to 2 decimal places in a 10-character field −Formatter f = new Formatter(); System.out.println(f.format("%10.2e", 3989.7886));The following is the complete example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); System.out.println(f.format("%08d", 697)); f = new Formatter(); System.out.println(f.format("%03d", 9878)); f = new Formatter(); System.out.println(f.format("%10.2e", 3989.7886)); } }Output00000697 9878 3.99e+03

Java Program to display table with columns in the output using Formatter

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

760 Views

To display table with columns as output, use the Formatter class. For working with Formatter class, import the following package −import java.util.Formatter;Consider an array with some elements. This is the array which we will be shown as output in tabular form −double arr[] = { 1.7, 2.5, 3.1, 4.5, 5.7, 6.9, 7.7, 8.9, 9.1 };While displaying the double array values, use the %f to set spaces −for (double d : arr) { f.format("%14.2f %14.2f %15.2f", d, Math.ceil(d), Math.floor(d)); }The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String[] ... Read More

Set a Minimum Field Width in Java

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

1K+ Views

The minimum field width specifier is what you include between % and the format conversion code.To include a 0 before the field width specifier, pad with 0's. An integer between the % sign and the format conversion code acts as a minimum field width specifier.Let us see an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); System.out.println(f.format("%08d", 697)); f = new Formatter(); System.out.println(f.format("%10d", 9878)); f = new Formatter(); System.out.println(f.format("%06d", 697)); } }Output00000697 9878 000697

The # format flag in Java

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

262 Views

Apply the # flag to the %o, %x, %e, and %f format specifiers. If you want to display the hexadecimal number with a 0x prefix, then precede the %x specifier with #.Preceding the %x specifier with a #, the hexadecimal number will be printed with a 0x prefix.Let us see an example wherein $e is used. It includes a decimal point, even if the decimal digits are not present −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); System.out.println(f.format("%#e", 5F)); } }Output5.000000e+00

Add grouping specifiers for large numbers in Java

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

160 Views

For using the Formatter class, import the following package −import java.util.Formatter;We can group specifiers as shown below −Formatter f = new Formatter(); f.format("%,.2f", 38178.9889);The above sets thousands separator and 2 decimal places.The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("%d", 50); System.out.println(f); f = new Formatter(); f.format("%,.2f", 38178.9889); System.out.println(f); } }Output50 38,178.99

Formatting a Negative Number Output with Parentheses in Java

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

479 Views

A negative number output can be shown using the Formatter object −Formatter f = new Formatter(); f.format("%12.2f", -7.598); System.out.println(f);Try the below given code to format a Negative Number Output with Parentheses −Formatter f = new Formatter(); f.format("%(d", -50); System.out.println(f);The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", 50); System.out.println(f); // negative number inside parentheses f = new Formatter(); f.format("%(d", -50); System.out.println(f); } }Output50 (50)

Space format specifiers in Java

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

1K+ Views

For format specifier, import the following package −import java.util.Formatter;Create a formatter object and set space format specifier −Formatter f = new Formatter(); f.format("% d", -50); System.out.println(f); f = new Formatter(); f.format("% d", 90); System.out.println(f); f = new Formatter(); f.format("%10d", -50); System.out.println(f); f = new Formatter(); f.format("% 10d", 90); System.out.println(f);The following is an example displaying different forms of space format specifiers −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", -50); ... Read More

Left justify output in Java

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

2K+ Views

Including a minus sign after the %, makes it left justified.Note − By default, output is right justifiedFirstly, create a formatter object −Formatter f = new Formatter();Now, use the format() method to left justify output −f.format("|%-15.5f|", 299.675796)The following is an example −Example Live Demoimport java.util.*; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); // left justify f = new Formatter(); System.out.println(f.format("|%-15.5f|", 299.675796)); } }Output|299.67580 |

Advertisements