Found 2616 Articles for Java

How do we mix two strings and generate another in java?

Maruthi Krishna
Updated on 10-Oct-2019 08:20:46

14K+ Views

Strings are used to store a sequence of characters in Java, they are treated as objects. The String class of the java.lang package represents a String.You can create a String either by using the new keyword (like any other object) or, by assigning value to the literal (like any other primitive datatype).String stringObject = new String("Hello how are you"); String stringLiteral = "Welcome to Tutorialspoint";Concatenating StringsYou can concatenate Strings in Java in the following ways −Using the "+" operator − Java Provides a concatenation operator using this, you can directly add two String literalsExampleimport java.util.Scanner; public class StringExample {   ... Read More

How do we create a string from the contents of a file in java?

Maruthi Krishna
Updated on 10-Oct-2019 07:24:03

138 Views

In Java you can read the contents of a file in several ways one way is to read it to a string using the java.util.Scanner class, to do so, Instantiate the Scanner class, with the path of the file to be read, as a parameter to its constructor.Create an empty String buffer.Start a while loop with condition, if the Scanner has next line. i.e. hasNextLine() at while.Within the loop append each line of the file to the StringBuffer object using the append() method.Convert the contents of the contents of the buffer to String using the toString() method.ExampleCreate a file with ... Read More

How do we split a string on a fixed character sequence in java?

Maruthi Krishna
Updated on 10-Oct-2019 08:19:19

484 Views

The split() method of the String class accepts a delimiter (in the form of the string), divides the current String into smaller strings based on the delimiter and returns the resulting strings as an array. If the String does not contain the specified delimiter this method returns an array which contains only the current string.For example if you pass single space “ ” as a delimiter to this method and try to split a String. This method considers the word between two spaces as one token and returns an array of words (between spaces) in the current String.If the String ... Read More

How to find a unique character in a string using java?

Maruthi Krishna
Updated on 10-Oct-2019 13:10:38

14K+ Views

You can find whether the given String contains specified character in the following ways −Using the indexOf() methodYou can search for a particular letter in a string using the indexOf() method of the String class. This method returns an integer parameter which is a position index of a word within the string or, -1 if the given character does not exist in the specified String.Therefore, to find whether a particular character exists in a String −Invoke the indexOf() method on the String by passing the specified character as a parameter.If the return value of this method is not -1 then ... Read More

How do we extract all the words start with a vowel and length equal to n in java?

Maruthi Krishna
Updated on 10-Oct-2019 06:58:55

1K+ Views

To find a word starts with a vowel letter −The split() method of the String class Split the given string into an array of Strings using the split() method of the String class.In the for loop traverse through each word of the obtained array.Get the first character of each word in the obtained array using the charAt() method.Verify if the character is equal to any of the vowels using the if loop if so print the word.ExampleAssume we have a text file with the following content −Tutorials Point originated from the idea that there exists a class of readers who ... Read More

How to extract an HTML tag from a String using regex in Java?

Maruthi Krishna
Updated on 17-Oct-2019 15:52:40

3K+ Views

The java.util.regex package of java provides various classes to find particular patterns in character sequences.The pattern class of this package is a compiled representation of a regular expression. To match a regular expression with a String this class provides two methods namely −compile() − This method accepts a String representing a regular expression and returns an object of the class Pattern.matcher() − This method accepts a String value and creates a matcher object which matches the given String to the pattern represented by the current pattern object.The Matcher class of java.util.regex package is an engine that performs match operations. To ... Read More

What is ARM in Java?

Maruthi Krishna
Updated on 10-Oct-2019 08:59:24

778 Views

A resource is an object which implements AutoClosable interface. whenever you use a resource in your program it is recommended to close it after the usage.Initially, this task is done using the finally block.Exampleimport java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner; public class FinalExample {    public static void main(String[] args) throws IOException {       File file = null;       FileInputStream inputStream = null;       try {          file = new File("D:\source\sample.txt");          inputStream = new FileInputStream(file);          Scanner sc = new Scanner(inputStream);     ... Read More

What is the role of the String intern() method in java?

Maruthi Krishna
Updated on 10-Oct-2019 08:55:14

94 Views

A String is a class in Java which stores a sequence of characters, it belongs to the java.lang package. Once you create a String object you cannot modify them (immutable).StorageAll String objects are stored in a separate memory location in the heap area known as, String Constant pool.Whenever you define a String value JVM creates a String object with the given value in the String constant pool. Therefore, if you run the above program two String values are created in the String constant pool.The intern() methodThis method returns value of the current String from the pool of unique String values. ... Read More

What are the rules of exception handling with respect to method overriding in java?

Maruthi Krishna
Updated on 10-Oct-2019 08:27:41

626 Views

While a superclass method throws an exception while overriding it you need to follow the certain rules.Should throw Same exception or, sub typeIf the super-class method throws certain exception, the method in the sub-class should throw the same exception or its sub type.ExampleIn the following example, the readFile() method of the super-class throws an IOEXception and, the readFile() method of the sub-class throws FileNotFoundException exception.Since the FileNotFoundException exception is the sub type of the IOException this program gets compiled and executed without any errors.import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; abstract class Super {    public String readFile(String path) ... Read More

How do we split a string with any whitespace chars as delimiters using java?

Maruthi Krishna
Updated on 10-Oct-2019 08:26:17

631 Views

The split() method of the String class accepts a delimiter (in the form of the string), divides the current String into smaller strings based on the delimiter and returns the resulting strings as an array. If the String does not contain the specified delimiter this method returns an array which contains only the current string.If the String does not contain the specified delimiter this method returns an array containing the whole string as element.Splitting the string with white space as delimiterTo split a String into an array of strings with white pace as delimiter −Read the source string.Invoke split() method ... Read More

Advertisements