Found 34494 Articles for Programming

Check whether the entered value is a letter or not in Java

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

5K+ Views

To check whether the entered value is a letter or not in Java, use the Character.isLetter() method.We have a value to be checked.char val = 'D';Now let us use the Character.isLetter() method.if (Character.isLetter(val)) {    System.out.println("Character is in Lowercase!"); }else {    System.out.println("Character is in Uppercase!"); }Let us see the complete example now to check for letter.Example Live Demopublic class Demo {    public static void main(String []args) {       System.out.println("Checking whether the given value is a Letter or not...");       char val = 'D';       System.out.println("Value: "+val);       if (Character.isLetter(val)) {   ... Read More

Java Program to validate if a String contains only numbers

karthikeya Boyini
Updated on 26-Jun-2020 12:17:33

1K+ Views

To validate if a String has only numbers, you can try the following codes. We have used the matches() method in Java here to check for number in a string.Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "978";       System.out.println("Checking for string that has only numbers...");       System.out.println("String: "+str);       if(str.matches("[0-9]+") && str.length() > 2)       System.out.println("String has only numbers!");       else       System.out.println("String consist of characters as well!");    } }OutputChecking for string that has only numbers... ... Read More

Java Program to subtract integers and check for overflow

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

890 Views

To check for Integer overflow, we need to check the Integer.MAX_VALUE with the subtracted integers result, Here, Integer.MAX_VALUE is the maximum value of an integer in Java.Let us see an example wherein integers are subtracted and if the result is more than Integer.MAX_VALUE, then an exception is thrown.The following is an example showing how to check for Integer overflow.Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 9898999;       int val2 = 8784556;       System.out.println("Value1: "+val1);       System.out.println("Value2: "+val2);       long sub = ... Read More

Check whether a character is Uppercase or not in Java

karthikeya Boyini
Updated on 06-Sep-2023 21:10:01

39K+ Views

To check whether a character is in Uppercase or not in Java, use the Character.isUpperCase() method.We have a character to be checked.char val = 'K';Now let us use the Character.isUpperCase() method.if (Character.isUpperCase(val)) {    System.out.println("Character is in Uppercase!"); }else {    System.out.println("Character is in Lowercase!"); }Let us see the complete example now to check for Uppercase in Java.Example Live Demopublic class Demo {    public static void main(String []args) {       System.out.println("Checking for Uppercase character...");       char val = 'K';       System.out.println("Character: "+val);       if (Character.isUpperCase(val)) {          System.out.println("Character is ... Read More

Java Program to display printable characters

Samual Sam
Updated on 26-Jun-2020 10:52:23

513 Views

To display printable characters, you need to work with the ASCII values from 32 to 127.With that, we are using the following, instead of System.out.println()System.out.write();The following displays all the printable characters.for (int i = 32; i < 127; i++) { System.out.write(i);Let us see the complete example.Example Live Demopublic class Demo {    public static void main(String []args) {       System.out.println("Printable characters...");       for (int i = 32; i < 127; i++) {          System.out.write(i);          if (i % 8 == 7)          System.out.write('');          else ... Read More

How to assign int value to char variable in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:52:45

2K+ Views

To assign int value to a char variable in Java would consider the ASCII value and display the associated character/ digit.Here, we have a char.char val;Now assign int value to it.val = 77;Now, when you will print the value of “val”, it would display the character associate with the value (ASCII) 77.The following is the complete example.Example Live Demopublic class Demo {    public static void main(String []args) {       char val;       val = 77;       System.out.print("Value: ");       System.out.println(val);    } }OutputValue: M

Reverse an Integer in Java

Samual Sam
Updated on 26-Jun-2020 10:53:18

1K+ Views

To reverse an integer in Java, try the following code −Example Live Demoimport java.lang.*; public class Demo {    public static void main(String[] args) {       int i = 239, rev = 0;       System.out.println("Original: " + i);       while(i != 0) {          int digit = i % 10;          rev = rev * 10 + digit;          i /= 10;       }       System.out.println("Reversed: " + rev);    } }OutputOriginal: 239 Reversed: 932In the above program, we have the following int value, which we will reverse.int i = 239;Now, loop through until the value is 0. Find the remainder and perform the following operations to get the reverse of the given integer 239.while(i != 0) {    int digit = i % 10;    rev = rev * 10 + digit;    i /= 10; }

Compare Two Java int Arrays in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:53:52

447 Views

To compare two Java short arrays, use the Arrays.equals() method.Let us first declare and initialize some int arrays.int[] arr1 = new int[] { 33, 66, 12, 56, 99 }; int[] arr2 = new int[] { 33, 66, 12, 56, 99 }; int[] arr3 = new int[] { 33, 66, 15, 56, 90 };Now let us compare any two of the above arrays.Arrays.equals(arr1, arr2));In the same way, work it for other arrays and compare themExample Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       int[] arr1 = new int[] { 33, 66, 12, 56, ... Read More

Pass an integer by reference in Java

Samual Sam
Updated on 26-Jun-2020 10:54:21

961 Views

To pass an integer by reference in Java, try the following code −Example Live Demopublic class Demo {    public static void display(int[] arr) {       arr[0] = arr[0] + 50;;    }    public static void main(String[] args) {       int val = 50;       int[] myArr = { val };       display(myArr);       System.out.println(myArr[0]);    } }Output100In the above program, a custom method is created, which pass an int array.int val = 50; int[] myArr = { val }; display(myArr);In the method, we have performed mathematical operation on the value of array.public static void display(int[] arr) {    arr[0] = arr[0] + 50;; }The updated value is then displayed in the main.System.out.println(myArr[0]);

Parse and format to hexadecimal in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:54:48

216 Views

To parse and format to hexadecimal in Java, we have used the BigInteger class. The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.In the below example, we have taken BigInteger and created an object. With that, some values have been set by us in the arguments as the hexadecimal and the radix i.e. 16 for hexadecimal.BigInteger bi = new BigInteger("2ef", 16);Let us see an example.Example Live Demoimport java.math.*; public class Demo {    public static void main( String args[] ) {       BigInteger bi = new BigInteger("2ef", 16); ... Read More

Advertisements