Found 33676 Articles for Programming

Make the first letter caps and the rest lowercase in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:30:03

1K+ Views

The following is an example −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "laptop";       System.out.println("Original String = " +str);       // letter one       String strOne = str.substring(0,1).toUpperCase();       // remaining letters       String strTwo = str.substring(1).toLowerCase();       System.out.println("Resultant String = "+strOne + strTwo);    } }OutputOriginal String = laptop Resultant String = Laptop

Left pad a String in Java with zeros

Samual Sam
Updated on 29-Jun-2020 05:30:58

585 Views

The following is our string −String str = "Tim";Now take a StringBuilder object −StringBuilder strBuilder = new StringBuilder();Perform left padding and extend the string length. We have set it till 20, that would include the current string as well. The zeros that will be padded comes on the left. Append the zeros here −while (strBuilder.length() + str.length() < 10) { strBuilder.append('0'); }The following is an example −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Tim";       StringBuilder strBuilder = new StringBuilder();       while (strBuilder.length() + str.length() ... Read More

Left pad an integer in Java with zeros

karthikeya Boyini
Updated on 29-Jun-2020 05:32:26

2K+ Views

Let us see an example first to understand how an integer looks with left padding −888 //left padding with spaces 0000000999 //left padding with 7 zerosLet us see an example to left pad a number with zero −Example Live Demopublic class Demo {    public static void main(String[] args) {       int val = 9899;       System.out.println(String.format("%05d",val));    } }Output09899Let us see another example that pads a greater number of zeros −Example Live Demopublic class Demo {    public static void main(String[] args) {       int val = 9899;       System.out.println(String.format("%010d", val));    } }Output0000009899

Left pad a String with spaces (' ') in Java

Samual Sam
Updated on 29-Jun-2020 05:33:33

330 Views

Let us see an example first to understand how a string looks with left padding −demotext //left padding with spaces 0000000demotext //left padding with 7 zerosThe following is our string −String str = "Jack";Now take a StringBuilder object −StringBuilder strBuilder = new StringBuilder();Perform left padding and extend the string length. The spaces that will be padded comes on the left. Append the spaces here −while (strBuilder.length() + str.length() < 10) {    strBuilder.append(' '); }The following is an example to pad a string to the left with spacesExample Live Demopublic class Demo {    public static void main(String[] args) {   ... Read More

Left pad a String with a specified character in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:34:29

280 Views

The following is our string −String str = "Amit";Now take a StringBuilder object −StringBuilder strBuilder = new StringBuilder();Set the character you want to pad with a string −Character ch = '^';Perform left padding and extend the string length to 5 (one for the additional character). The character that will be padded comes on the left −while (strBuilder.length() + str.length() < 5) { strBuilder.append(ch); }The following is an example −Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "Amit";       Character ch = '^';       StringBuilder strBuilder = ... Read More

Java Program to implement andNot operation on BigInteger

Samual Sam
Updated on 29-Jun-2020 05:35:51

136 Views

TheBigInteger.andNot(BigInteger val) returns a BigInteger whose value is (this & ~val). This method, which is equivalent to and(val.not()), is provided as a convenience for masking operations. This method returns a negative BigInteger if and only if this is negative and val is positive. Here, “val” is the value to be complemented and AND'ed with this BigInteger.The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two, three;       one = new BigInteger("12");       two = new BigInteger("6");       three = ... Read More

Java Program to implement OR operation on BigInteger

karthikeya Boyini
Updated on 29-Jun-2020 05:37:00

164 Views

TheBigInteger.or(BigInteger val) returns a BigInteger whose value is (this | val). This method returns a negative BigInteger if and only if either this or val is negative.Here, “val” is the value to be OR'ed with this BigInteger.The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two, three;       one = new BigInteger("6");       two = one.or(one);       System.out.println("Result (or operation): " +two);    } }OutputResult (or operation): 6Let us see another example −Example Live Demoimport java.math.*; public class Demo { ... Read More

Java Program to implement NOT operation on BigInteger

Samual Sam
Updated on 29-Jun-2020 05:38:35

144 Views

The BigInteger.not() method returns a BigInteger whose value is (~this). This method returns a negative value if and only if this BigInteger is non-negative.The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two, three;       one = new BigInteger("6");       two = one.not();       System.out.println("Result (not operation): " +two);    } }OutputResult (not operation): -7Let us see another example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2, ... Read More

Java Program to perform AND operation on BigInteger

Sharon Christine
Updated on 29-Jun-2020 05:40:35

143 Views

The java.math.BigInteger.and(BigInteger val) returns a BigInteger whose value is (this & val). This method returns a negative BigInteger if and only if this and val are both negative. Here, “val” is the value to be AND'ed with this BigInteger.First, create BigInteger objects −BigInteger one, two, three; one = new BigInteger("12"); two = new BigInteger("6");Perform AND operation on first and second object −three = one.and(two);The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two, three;       one = new BigInteger("12");       two ... Read More

Java Program to perform XOR operation on BigInteger

karthikeya Boyini
Updated on 29-Jun-2020 05:41:49

229 Views

The java.math.BigInteger.xor(BigInteger val) returns a BigInteger whose value is (this ^ val). This method returns a negative BigInteger if and only if exactly one of this and val are negative. Here, “val” is the value to be XOR'ed with this BigInteger.Firstly, create two BigInteger objects −one = new BigInteger("6"); two = new BigInteger("-5");Now, perform XOR −three = one.xor(two);The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two, three;       one = new BigInteger("6");       two = new BigInteger("-5");       ... Read More

Advertisements