Found 34494 Articles for Programming

Convert Integer to Hex String in Java

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

12K+ Views

The Integer.toHexString() method in Java converts Integer to hex string.Let’s say the following are our integer values.int val1 = 5; int val2 = 7; int val3 = 13;Convert the above int values to hex string.Integer.toHexString(val1); Integer.toHexString(val2); Integer.toHexString(val3);Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 5;       int val2 = 7;       int val3 = 13;       int val4 = 23;       int val5 = 30;       System.out.println("Converting integer "+val1+" to Hex String: "+Integer.toHexString(val1));       System.out.println("Converting integer "+val2+" to ... Read More

Convert a String to a byte number in Java

Samual Sam
Updated on 26-Jun-2020 16:27:46

162 Views

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

Convert string of time to time object in Java

karthikeya Boyini
Updated on 26-Jun-2020 15:31:06

8K+ Views

Here is our string.String strTime = "20:15:40";Now, use the DateFormat to set the format for date.DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");Parse the string of time to time object.Date d = dateFormat.parse(strTime);The following is the complete example.Example Live Demoimport java.text.DateFormat; import java.util.Date; import java.text.SimpleDateFormat; public class Demo {     public static void main(String[] args) throws Exception {        String strTime = "20:15:40";        DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");        Date d = dateFormat.parse(strTime);        System.out.println("Resultant Date and Time = " + d);     } }OutputResultant Date and Time = Thu Jan 01 20:15:40 UTC 1970

Java Program to convert String to Integer using Integer.parseInt()

Samual Sam
Updated on 26-Jun-2020 15:31:54

188 Views

The Integer.parseInt() method in Java converts a string to an integer.Let’s say the following is our string −String str = "456";Converting it into integer −Integer.parseInt(str));The following is the complete example −Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "456";        System.out.println(Integer.parseInt(str));     } }Output456

Java Program to convert a string into a numeric primitive type using Integer.valueOf()

karthikeya Boyini
Updated on 26-Jun-2020 15:33:54

455 Views

The Integer.valueOf() method is used in Java to convert a string into a numeric primitive type.Let’s say the following is our string.String str = "989";To convert it into Integer primitive type, use Integer.valueOf()Integer.valueOf(str)Let us see the complete example to convert a string to Integer primitive type.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "989";        System.out.println(Integer.valueOf(str));     } }Output989

Narrowing Conversion in Java

Samual Sam
Updated on 26-Jun-2020 16:09:51

2K+ Views

Narrowing conversion is needed when you convert from a larger size type to a smaller size. This is for incompatible data types, wherein automatic conversions cannot be done.Let us see an example wherein we are converting long to integer using Narrowing Conversion.Example Live Demopublic class Demo {     public static void main(String[] args) {        long longVal = 878;        int intVal = (int) longVal;        System.out.println("Long: "+longVal);        System.out.println("Integer: "+intVal);     } }OutputLong: 878 Integer: 878Let us see another example, wherein we are converting double to long using Narrowing Conversion.Example Live Demopublic class Demo {     public static void main(String[] args) {        double doubleVal = 299.89;        long longVal = (long)doubleVal;       ... Read More

Java Program to set a range for displaying substring

karthikeya Boyini
Updated on 26-Jun-2020 16:10:40

1K+ Views

Use the substring() method to set a range of substring from a string. Let’s say the following is our string.String str = "pqrstuvw";Getting the range of string from 3 to 6String strRange = str.substring(3, 6);The following is the complete example wherein we have set a range for displaying substring from a string.Example Live Demopublic class Demo {     public static void main(String[] args) {        String str = "pqrstuvw";        System.out.println("String: "+str);        // range from 3 to 6        String strRange = str.substring(3, 6);        System.out.println("Substring: "+strRange);     } }OutputString: pqrstuvw Substring: stu

Java Program to search for last index of a group of characters

Samual Sam
Updated on 26-Jun-2020 16:15:37

132 Views

Use the lastIndexOf() method to search for last index of a group of characters. Let’s say the following is our string.String myStr = "pqrstuvwxyzpqrst";Searching for the substring “pqrs” in the string to get the last index of its occurring in the string. We begin the search from index 3.int begnIndex = 3; strLastIndex = myStr.indexOf("pqrs", begnIndex);The following is an example, wherein we are searching for the last index of the substring “pqrs”Example Live Demopublic class Demo {     public static void main(String[] args) {        String myStr = "pqrstuvwxyzpqrst";        int strLastIndex = 0;        System.out.println("String: "+myStr);        strLastIndex = myStr.lastIndexOf("pqrs");        System.out.println("The last index of ... Read More

Java Program to search for a Substring from a specified index

karthikeya Boyini
Updated on 26-Jun-2020 15:08:13

281 Views

Use the indexOf() method to search for a substring from a given position.Let’s say the following is our string.String myStr = " pqrstuvwxyzpqrst";Searching for the substring “pqrs” in the string. We are beginning the index from 3 for our search.int begnIndex = 3; strLastIndex = myStr.indexOf("pqrs", begnIndex);Example Live Demopublic class Demo {     public static void main(String[] args) {        String myStr = "pqrstuvwxyzpqrst";        int strLastIndex = 0;        int begnIndex = 3;        System.out.println("String: "+myStr);        strLastIndex = myStr.indexOf("pqrs",  begnIndex);        System.out.println("The index of substring pqrs in the string beginning from index "+begnIndex+" = "+strLastIndex);     } }OutputString: pqrstuvwxyzpqrst The index of substring pqrs ... Read More

Search for a character from a given position in Java

Samual Sam
Updated on 26-Jun-2020 15:10:01

102 Views

Use the indexOf() method to search for a character from a given position.Let’s say the following is our string.String myStr = "Amit Diwan";Here, we are searching for the character “i” in the string. We are beginning the index from 4 for our search.int begnIndex = 4; System.out.println("String: "+myStr); strLastIndex = myStr.indexOf('i', begnIndex);The following is the complete example.Example Live Demopublic class Demo {    public static void main(String[] args) {       String myStr = "Amit Diwan";       int strLastIndex = 0;       int begnIndex = 4;       System.out.println("String: "+myStr);       strLastIndex = myStr.indexOf('i',  begnIndex);       System.out.println("The index of character a in the string beginning from index "+begnIndex+" ="+strLastIndex);   ... Read More

Advertisements