Found 7442 Articles for Java

Java program to remove a character at a specified position

karthikeya Boyini
Updated on 16-Sep-2024 23:26:05

2K+ Views

In this article, we will learn to remove a character from a string at a specified position using Java. We will use the substring() method to achieve this by splitting the string before and after the character we want to remove, then combining the two parts Problem Statement Write a program in Java to remove a character at a specified position. Input The Haunting of Hill House! Output String after removing a character: The Hauting of Hill House! Steps to remove a character at a specified position Following are the steps to remove a character at a specified position − ... Read More

Java Program to remove leading and trailing whitespace from a string

Samual Sam
Updated on 26-Jun-2020 09:22:53

454 Views

Use the Trim() method to remove leading and trailing whitespace from a string. Let’s say the following is our string with white spaces in the beginning and the end.String str = " a ";Now, use the trim() method.str.trim()The following is the final example with output.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = " a ";       System.out.println("Text = "+str);       System.out.println("Text after using trim() method = " + str.trim());     } }OutputText = a Text after using trim() method = a

Java Program to convert integer to hexadecimal

karthikeya Boyini
Updated on 26-Jun-2020 09:24:33

6K+ Views

Use the + Integer.toHexString() method in Java to convert integer to hexadecimal.Let’s say the following is our integer.int val = 768;Let us convert it to a hexadecimal value.Integer.toHexString(val)The following is the final example with the output.Example Live Demopublic class Demo {     public static void main(String[] args) {        int val = 768;        // integer        System.out.println("Integer: "+val);        // hex        System.out.println("Hex String = " + Integer.toHexString(val));     } }OutputInteger: 768 Hex String = 300

Java program to convert integer to octal

Samual Sam
Updated on 15-Oct-2024 10:39:56

3K+ Views

In Java, converting integers to different numeral systems can be easily achieved using built-in methods. The Integer.toOctalString(int) method allows you to convert an integer to its octal string representation. This is particularly useful in applications that require data representation in octal format. To convert integer to octal in Java, use the Integer.toOctalString() method. Problem Statement Given a set of integer values, we need to convert each integer to its octal string representation using the Integer.toOctalString(int) method. Input int val = 768; Output Octal String = 1400 Step to convert the integer to octal Below are the steps to convert integer ... Read More

Java Program to convert an integer into binary

karthikeya Boyini
Updated on 21-Jun-2024 13:59:18

13K+ Views

To convert an integer to binary, use the Integer.toBinaryString() method in Java.Let’s say the following is the integer we want to convert.int val = 999;Converting the above integer to binary.Integer.toBinaryString(val)Example Live Demopublic class Demo {     public static void main(String[] args) {        int val = 999;        // integer        System.out.println("Integer: "+val);        // binary        System.out.println("Binary = " + Integer.toBinaryString(val));     } }OutputInteger: 999 Binary = 1111100111

Java NumberFormat.getPercentageInstance() Method

Samual Sam
Updated on 28-Jan-2025 14:58:02

613 Views

In this article, we will learn the NumberFormat.getPercentageInstance() method. Java provides powerful tools for formatting numbers, currencies, and percentages through the java.text.NumberFormatclass. One of the most useful methods in this class is the getPercentageInstance() method. NumberFormat.getPercentageInstance() method The NumberFormat.getPercentageInstance() method is a static method that returns a NumberFormat instance configured to format numbers as percentages. This method can be used with or without a Locale parameter.  Syntax − public static NumberFormat getPercentageInstance()public static NumberFormat getPercentageInstance(Locale locale) Without Locale: If no locale is specified, the method uses the default locale of the JVM. ... Read More

Java NumberFormat.getCurrencyInstance() method

karthikeya Boyini
Updated on 26-Jun-2020 09:10:36

4K+ Views

The getCurrencyInstance() method of the NumberFormat class returns the instance of the NumberFormat class. The java.text.NumberFormat class is used for formatting numbers and currencies as per a specific Locale. Number formats varies from country to countryHere, we have considered a locale.NumberFormat n = NumberFormat.getCurrencyInstance(Locale.FRANCE);Then, we have formatted a double value with the currency.double points = 1.78; System.out.println(n.format(points));The following is the final example.Example Live Demoimport java.text.NumberFormat; import java.util.Locale; public class MainClass {     public static void main(String[] args) {        // Currency of France is Euro        NumberFormat n = NumberFormat.getCurrencyInstance(Locale.FRANCE);        // points        double points = 1.78;        double totalPoints = points * 1000;        System.out.println(n.format(points));     ... Read More

Java NumberFormat.getInstance() method

Samual Sam
Updated on 04-Nov-2024 18:41:47

560 Views

In this article, we will learn how to format numbers in Java using the NumberFormat class from the java.text package. The NumberFormat class provides methods for formatting numbers based on Locale settings, which is useful for creating internationalized Java applications. Using the NumberFormat.getInstance() method, we can format double, int, or float values in a Java program.  Problem StatementGiven a decimal value, we need to format the number using Java's NumberFormat class to round it according to the default Locale settings.Input double val = 1.9898798; Output Formatted Value: 1.99 ... Read More

Convert a String to a short number in Java

karthikeya Boyini
Updated on 26-Jun-2020 16:21:34

299 Views

Use the Short.parseShort() method in Java to convert a String to a short.Let’s say the following is our string.String str = “76”;Now, the parseShort() method will convert the string to short.byte val = Byte.parseByte(str);Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "76";        short val = Short.parseShort(str);        System.out.println(val);     } }Output76

Java Program to add leading zeros to a number

Samual Sam
Updated on 26-Jun-2020 09:15:38

20K+ Views

To add leading zeros to a number, you need to format the output. Let’s say we need to add 4 leading zeros to the following number with 3 digits.int val = 290;For adding 4 leading zeros above, we will use %07d i.e. 4+3 = 7. Here, 3, as shown above, is the number with 3 digits.String.format("%07d", val);The following is the final example.Example Live Demoimport java.util.Formatter; public class Demo {     public static void main(String args[]) {        int val = 290;        System.out.println("Integer: "+val);        String formattedStr = String.format("%07d", val);        System.out.println("With leading zeros = " + formattedStr);     } }OutputInteger: 290 With leading zeros = 0000290

Advertisements