Found 34489 Articles for Programming

Concatenate string to an int value in Java

Naveen Singh
Updated on 22-Oct-2023 03:25:06

25K+ Views

To concatenate a string to an int value, use the concatenation operator.Here is our int.int val = 3;Now, to concatenate a string, you need to declare a string and use the + operator.String str = "Demo" + val;Let us now see another example.Example Live Demoimport java.util.Random; public class Demo {    public static void main( String args[] ) {       int val = 3;       String str = "" + val;       System.out.println(str + " = Rank ");    } }Output3 = Rank

Java Program to convert a String to int

Naveen Singh
Updated on 10-Aug-2023 13:06:22

294 Views

To convert a String to int in Java, we can use the two built-in methods namely parseInt() and valueOf(). These static methods belong to the Integer class of java.lang package and throws a NumberFormatException if the string is not a valid representation of an integer. In Java, String is a class of 'java.lang' package that stores a series of characters enclosed within double quotes. And, an integer is a primitive datatype that stores numerical values. In this article, we will discuss a few Java programs that illustrate how to convert a given String to integer. Java Program to Convert a ... Read More

Check two numbers for equality in Java

Naveen Singh
Updated on 26-Jun-2020 10:44:37

10K+ Views

To check two numbers for equality in Java, we can use the Equals() method as well as the == operator.Firstly, let us set Integers.Integer val1 = new Integer(5); Integer val2 = new Integer(5);Now, to check whether they are equal or not, let us use the == operator.(val1 == val2)Let us now see the complete example.Example Live Demopublic class Demo {    public static void main( String args[] ) {       Integer val1 = new Integer(5);       Integer val2 = new Integer(5);       Integer val3 = new Integer(10);       System.out.println("Integer 1 = "+val1);   ... Read More

Integer Equals() method in Java

Naveen Singh
Updated on 26-Jun-2020 10:45:13

8K+ Views

The Equals() method compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.Let us first set Integer object.Integer val1 = new Integer(30); Integer val2 = new Integer(60); Integer val3 = new Integer(55); Integer val4 = new Integer(30);Now let us check their equality using the Equals() method.val1.equals(val2);In the same way, check it for different Integers.Let us see the complete example.Example Live Demoimport java.util.Random; public class Demo {    public static void main( String args[] ) {     ... Read More

Random number generator in Java

Naveen Singh
Updated on 26-Jun-2020 10:45:38

2K+ Views

To generate random numbers in Java, use.import java.util.Random;Now, take Random class and create an object.Random num = new Random();Now, in a loop, use the nextInt() method since it is used to get the next random integer value. You can also set a range, like for 0 to 20, write it as.nextInt( 20 );Let us see the complete example wherein the range is 1 to 10.Example Live Demoimport java.util.Random; public class Demo {    public static void main( String args[] ) {       Random num = new Random();       int res;       for ( int i = 1; i

Read integers from console in Java

Naveen Singh
Updated on 26-Jun-2020 10:36:26

18K+ Views

To read integers from console, use Scanner class.Scanner myInput = new Scanner( System.in );Allow a use to add an integer using the nextInt() method.System.out.print( "Enter first integer: " ); int a = myInput.nextInt();In the same way, take another input in a new variable.System.out.print( "Enter second integer: " ); Int b = myInput.nextInt();Let us see the complete example.Exampleimport java.util.Scanner; public class Demo {    public static void main( String args[] ) {       Scanner myInput = new Scanner( System.in );       int a;       int b;       int sum;       System.out.print( ... Read More

Java Program to convert Boolean to String

Naveen Singh
Updated on 26-Jun-2020 10:37:05

10K+ Views

To convert Boolean to String in Java, use the toString() method. For this, firstly, we have declared two booleans.boolean bool1 = false; boolean bool2 = true;Now, convert Boolean to String using the toString() method in Java as shown below −String str1 = new Boolean(bool1).toString(); String str2 = new Boolean(bool2).toString();Now, when you will display the values “str1” and “str2”, the output would be in String.Let us now see the complete example to convert Boolean to String.Example Live Demopublic class Demo {    public static void main(String[] args) {       boolean bool1 = false;       boolean bool2 = true; ... Read More

Java Program to convert String to Boolean

Naveen Singh
Updated on 21-Jun-2024 13:09:42

15K+ Views

To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".Firstly, declare a string.String str = "false";Now, use the Boolean.parseBoolean() method to convert the above declared String to Boolean.boolean bool = Boolean.parseBoolean(str);Let us now see the complete example to learn how to convert String to Boolean.Example: Convert String to Boolean Live Demopublic class Demo {    public static void main(String[] args) {       // string     ... Read More

Create a Boolean object from Boolean value in Java

Naveen Singh
Updated on 26-Jun-2020 10:37:57

2K+ Views

To create a Boolean object from Boolean value is quite easy. Create an object with Boolean and set the Boolean value “true” or “false”, which are the Boolean literals.Let us now see how “true” value is added.Boolean bool = new Boolean("true");In the same way, “False” value is added.Boolean bool = new Boolean("false");The following is an example displaying how to create a Boolean object from Boolean value.Example Live Demopublic class Demo {    public static void main(String[] args) {       Boolean bool = new Boolean("true");       System.out.println(bool);       bool = new Boolean("false");       System.out.println(bool); ... Read More

Convert Java String Object to Boolean Object

Naveen Singh
Updated on 26-Jun-2020 10:38:28

1K+ Views

A string object can be created in Java using the string literal.String myStr = “Amit”;Another way to create a string object is using the new keyword.String myStr = new String("Hello!");We have used the first method to create a string object.String str = "true";Now, use the valueOf() method to convert String Object to Boolean Object. We have used this method on Boolean object.Boolean bool = Boolean.valueOf(str);Let us now see the complete example to show how to convert String Object to Boolean Object.Example Live Demopublic class Demo {    public static void main(String[] args) {       String str = "true";   ... Read More

Advertisements