Found 2617 Articles for Java

What is a sub string in Java?

Maruthi Krishna
Updated on 05-Feb-2021 10:25:09

107 Views

The String class of the java.lang package represents set of characters. All string literals in Java programs, such as "abc", are implemented as instances of this class. A string index is an integer representing the position of each character in the string starting from zero.A substring is a part/segment of a string. You can identify a substring of a string using the substring() method of the String class. This method have two variants −substring(int beginIndex)This method accepts an integer value representing an index in the current string and returns the substring starting from the given index to the end of ... Read More

Sort an arrays of 0’s, 1’s and 2’s using Java

Dev Prakash Sharma
Updated on 05-Feb-2021 12:53:48

267 Views

Given an array of 0, 1 and 2 sort the element in an order such that all the zeros come first before 1 and all the 2’s in the end. We have to sort all the elements of the array in-place.We can solve this problem using DNF (Dutch National Flag) Sorting Algorithm. For example, Input-1 −arr[ ]= {2, 0, 0, 1, 2, 1 }Output −0 0 1 1 2 2Explanation − Sorting the given array of elements containing 0, 1 and 2 using the DNF Sorting Algorithm, it will print the output as {0, 0, 1, 1, 2, 2}.Input-2 −arr[ ... Read More

Write a program in Java to rotate a matrix by 90 degrees in anticlockwise direction

Dev Prakash Sharma
Updated on 05-Feb-2021 12:49:35

2K+ Views

Let’s suppose we have given a square matrix of N×N. The task is to rotate the matrix counterclockwise. For example, Input-1 −N = 3 matrix[ ][ ] = [    [1 2 3],    [4 5 6],    [7 8 9] ]Output −3 6 9 2 5 8 1 4 7Explanation: After rotating the matrix counterclockwise it will generate the output as, 3 6 9 2 5 8 1 4 7.Approach to solve this problemInitially the idea is to find the transpose of the given matrix and then swap each of the elements of the matrix while traversing row-wise.Take Input ... Read More

Majority Element in Java

Dev Prakash Sharma
Updated on 05-Feb-2021 12:43:28

869 Views

Let’s suppose we have given an array of integers. The task is to find the index of a particular element in the given array. For example, Input-1 −N = 8 A[ ] = { 1, 2, 4, 3, 3, 1, 1, 5}Output −1Explanation − In the given array of integers, the most appearing number is ‘1’. Thus the output is ‘1’.Input-2 −N = 6 A[ ] = {1, 5, 4, 4, 1, 1}Output −1Explanation − In the given array of integers, the most appearing number is ‘1’. Thus we can return the output ‘1’.Approach to solve this problemThe given array ... Read More

Write a program in Java to find the missing positive number in a given array of unsorted integers

Dev Prakash Sharma
Updated on 05-Feb-2021 12:15:59

459 Views

Let’s suppose we have given an array of unsorted integers. The task is to find the positive missing number which is not present in the given array in the range [0 to n]. For example, Input-1 −N = 9 arr = [0, 2, 5, 9, 1, 7, 4, 3, 6]Output −8Explanation − In the given unsorted array, ‘8’ is the only positive integer that is missing, thus the output is ‘8’.Input-2 −N = 1 arr = [0]Output −1Explanation − In the given array, ‘1’ is the only one positive integer which is missing, thus the output is ‘1’.Approach to solve ... Read More

Write a program in Java to check if a string can be obtained by rotating another string by 2 places

Dev Prakash Sharma
Updated on 05-Feb-2021 11:56:59

821 Views

Suppose we’ve two strings ‘a’ and ‘b’, the task is to find whether we can obtain string ‘b’ by rotating string ‘a’ exactly by 2 places in an anticlockwise or clockwise direction. For example, Input-1 −a = google b = legoogOutput −TrueExplanation − String ‘google’ can be rotated in an anticlockwise direction by two places, which results in the string ‘legoog’. Thus, we return True.Input-2 −a = tuorialst b = tutorialsOutput −FalseExplanation − String ‘tuorialst’ cannot be rotated by two places in any direction to get another string ‘tutorials’. Thus, we return False.The approach used to solve this problemFor the ... Read More

Count occurrences of a substring recursively in Java

Sunidhi Bansal
Updated on 05-Jan-2021 04:54:47

8K+ Views

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive process.A recursive function is the one which has its own call inside it’s definition.If str1 is “I know that you know that i know” str2=”know”Count of occurences is − 3Let us understand with examples.For ExampleInputstr1 = "TPisTPareTPamTP", str2 = "TP";OutputCount of occurrences of a substring recursively are: 4ExplanationThe substring TP occurs 4 times in str1.Inputstr1 = "HiHOwAReyouHiHi" str2 = "Hi"OutputCount of occurrences of a substring recursively are: 3ExplanationThe substring Hi occurs 3 times in str1.Approach used ... Read More

How to scroll down using Selenium WebDriver with Java?

Debomita Bhattacharjee
Updated on 30-Nov-2020 10:55:10

2K+ Views

We can scroll down with Selenium. Selenium is unable to handle scrolling directly. It takes the help of the Javascript Executor to perform the scrolling action up to an element.First of all we have to locate the element up to which we have to scroll to. Next, we shall use the Javascript Executor to run the Javascript commands. The method executeScript is used to run Javascript commands in Selenium. We shall take the help of the scrollIntoView method in Javascript and pass true as an argument to the method.SyntaxWebElement elm = driver.findElement(By.name("name")); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", elm);Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; ... Read More

How to simulate Print screen button using selenium webdriver in Java?

Debomita Bhattacharjee
Updated on 30-Nov-2020 10:50:15

368 Views

We can simulate a Print screen button with Selenium. The screenshot is captured with the Print screen button. Capturing a screenshot is a three way process. It is an important step towards failure analysis.We shall convert the driver object to TakeScreenshot interface.SyntaxTakesScreenshot s = (TakesScreenshot)driver;Then, with the getScreenshotAs method we shall have an image file and copy that file to a location with FileUtils.copyFile method.SyntaxFile sp=s.getScreenshotAs(OutputType.FILE); FileUtils.copyFile(sp, new File("path of image file"));Exampleimport org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.apache.commons.io.FileUtils; import java.io.File; public class PrintScreenSimulate {    public static void main(String[] args) {     ... Read More

Clear browser Cookies with Selenium WebDriver Java bindings.

Debomita Bhattacharjee
Updated on 30-Nov-2020 10:49:08

533 Views

We can clear browser cookies in Selenium. The method deleteCookieNamed shall delete a cookie with a specific name. The cookie named is passed as an argument to the method. First, we will add a cookie, then get it and finally delete it.Syntaxdriver.manage().deleteCookieNamed("foo");Another method called the deleteAllCookies deletes all cookies from the existing domain. First, we will add the cookies, then get and delete them.Syntaxdriver.manage().deleteAllCookies();Exampleimport java.util.Set; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class DeleteCookiesViaName{    public static void main(String[] args) {       System.setProperty("webdriver.chrome.driver",       "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");       WebDriver driver = new ChromeDriver();   ... Read More

Advertisements