Java Program to locate a substring in a string

karthikeya Boyini
Updated on 18-Jun-2024 18:39:39

14K+ Views

To locate a substring in a string, use the indexOf() method.Let’s say the following is our string.String str = "testdemo";Find a substring ‘demo’ in a string and get the index.int index = str.indexOf( 'demo');Example Live Demopublic class Demo {    public static void main(String []args) {       String str = "testdemo";       System.out.println("String: "+str);       int index = str.indexOf("demo");       System.out.printf("Substring 'demo' is at index %d", index);    } }OutputString: testdemo Substring 'demo' is at index 4

JavaScript Program to find simple interest

AmitDiwan
Updated on 18-Jun-2024 18:26:35

6K+ Views

We will use the formula for simple interest to calculate the interest for a given principal amount, rate of interest, and time period. Simple interest is calculated as the product of the principal amount, rate of interest, and time period. This formula makes it easy for us to find the interest amount without having to calculate the compound interest. In our JavaScript program, we will take input from the user for the principal amount, rate of interest, and time period, and then use the formula to calculate the simple interest. We will then display the result to the user. By ... Read More

Java Program to swap the case of a String

Samual Sam
Updated on 18-Jun-2024 18:01:46

12K+ Views

To swap the case of a string, use.toLowerCase() for title case string. toLowerCase() for uppercase stringtoUpperCase() for lowercase stringLoop through what we discussed above for all the characters in the sting.for (int i = 0; i > len; i++) {    c = str.charAt(i);    // title case converted to lower case    if (Character.isTitleCase(c)) {       c = Character.toLowerCase(c);    }    // upper case converted to lower case    if (Character.isUpperCase(c)) {       c = Character.toLowerCase(c);    }    // lower case converted to upper case    if (Character.isLowerCase(c)) {       c ... Read More

Java Program to Compute the Sum of Diagonals of a Matrix

AmitDiwan
Updated on 18-Jun-2024 17:51:56

8K+ Views

In this article, we will understand how to compute the sum of diagonals of a matrix. The matrix has a row and column arrangement of its elements. The principal diagonal is a diagonal in a square matrix that goes from the upper left corner to the lower right corner.The secondary diagonal is a diagonal of a square matrix that goes from the lower left corner to the upper right corner.Below is a demonstration of the same −Suppose our input is −The input matrix: 4 5 6 7 1 7 3 4 11 12 13 14 23 24 25 50The desired ... Read More

JavaScript/ Typescript object null check?

Nikhilesh Aleti
Updated on 18-Jun-2024 17:36:07

11K+ Views

In this article we will check if an object is a null in Typescript. A variable is undefined until and unless it is not assigned to any value after declaring it. NULL is known as empty or dosen’t exist. In typescript, unassigned values are by default undefined, so in order to make a variable null, we must assign it a null value. To check a variable is null or not in typescript we can use typeof or "===" operator. Using typeofoperator The typeof operator in JavaScript is used to find the datatype of the variable. Example In the example ... Read More

Java Program to retrieve the set of all keys and values in HashMap

karthikeya Boyini
Updated on 18-Jun-2024 17:04:46

14K+ Views

To retrieve the set of keys from HashMap, use the keyset() method. However, for set of values, use the values() method.Create a HashMap −HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));Now, retrieve the keys −Set keys = hm.keySet(); Iterator i = keys.iterator(); while (i.hasNext()) { System.out.println(i.next()); }Retrieve the values −Collection getValues = hm.values(); i = getValues.iterator(); while (i.hasNext()) { System.out.println(i.next()); }The following is an example to get the set of all keys and values in HashMap −Example Live Demoimport java.util.*; public class Demo { public static ... Read More

Java program to find the area of a rectangle

Samual Sam
Updated on 18-Jun-2024 16:59:45

10K+ Views

Area of a rectangle is the product of its length and breadth. Therefore, to calculate the area of a rectangleGet the length of the rectangle form the user.Get the breadth of the rectangle form the user.Calculate their product.Print the product.ExampleBelow is an example to find the area of a rectangle in Java using Scanner class.import java.util.Scanner; public class AreaOfRectangle {    public static void main(String args[]){       int length, breadth, area;       Scanner sc = new Scanner(System.in);       System.out.println("Enter the length of the rectangle ::");       length = sc.nextInt();       ... Read More

Java Program to fill an array of characters from user input

Samual Sam
Updated on 18-Jun-2024 16:45:18

14K+ Views

For user input, use the Scanner class with System.in. After getting the input, convert it to character array −char[] a = s.next().toCharArray();Now, display it until the length of the character array i.e. number of elements input by the user −for (int i = 0; i < a.length; i++) {    System.out.println(a[i]); }To fill an array of characters from user input, use Scanner class.Exampleimport java.util.Scanner; public class Demo {    public static void main(String args[]) {       Scanner s = new Scanner(System.in);       System.out.println("First add some characters...");       char[] a = s.next().toCharArray();       ... Read More

Java program to convert a list to an array

Lakshmi Srinivas
Updated on 18-Jun-2024 16:02:05

17K+ Views

The List object provides a method known as toArray(). This method accepts an empty array as argument, converts the current list to an array and places in the given array. To convert a List object to an array − Create a List object. Add elements to it. Create an empty array with size of the created ArrayList. Convert the list to an array using the toArray() method, bypassing the above-created array as an argument to it. Print the contents of the array.ExampleLive Demoimport java.util.ArrayList; public class ListToArray {    public static void main(String args[]){       ArrayList list = new ArrayList();       ... Read More

Java program to find a cube of a given number

Samual Sam
Updated on 18-Jun-2024 15:48:41

13K+ Views

Cube of a value is simply three times multiplication of the value with self.For example, cube of 2 is (2*2*2) = 8.AlgorithmSteps to find a cube of a given number in Java programming:Take integer variable A.Multiply A three times.Display result as Cube.Exampleimport java.util.Scanner; public class FindingCube {    public static void main(String args[]){       int n = 5;       System.out.println("Enter a number ::");       Scanner sc = new Scanner(System.in);       int num = sc.nextInt();       System.out.println("Cube of the given number is "+(num*num*num));    } }OutputEnter a number :: 5 Cube of the given number is 125

Advertisements