Find First and Last Word of a File Containing String in Java


As per the problem statement, we need to find the first and last word of the given String.

Let’s explore the article to see how it can be done by using Java programming language.

To Show You Some Instances

Instance-1

Suppose there is String “Java is a well-known high-level, class-based object-oriented programming language that Sun Microsystems created and first distributed in 1995. Over 3 billion devices run Java, which is currently owned by Oracle”

After printing the first and last word of this String i.e “Java” and “Oracle”.

Instance-2

Suppose there is String “For the creation of their desktop, web, and mobile apps, all of the big businesses are actively seeking to hire Java programmers”

After printing the first and last word of this String i.e “For” and “programmers”.

Instance-3

Suppose there is String “we presume that the readers have some familiarity with programming environments”

After printing the first and last word of this String i.e “we” and “environments”.

Algorithm

  • Step 1 − Create an object of BufferedReader class.

  • Step 2 − By using indexOf() method find index of first space(“ “). Then by using the substring() method find the first word. In substring method 0 and first space index will be the start and end index respectively.

  • Step 3 − Similarly, by using indexOf() method find index of last space(“ “). Then by using the substring() method find the last word. The substring() method (" ")+1 will give you the last word.

  • Step 4 − Finally, you will see the results printed on the console.

Syntax

To get the index of the first character present in the given String we use indexOf() method. If it’s not then it returns -1.

Below refers to the syntax of it −

int indexOf(char ch)

Where, ‘ch’ refers to the specific character of which you need the index position.

To extract a substring from the given string by using the index values we use the inbuilt substring() method of String class.

Below refers to the syntax of it −

str.substring(int startIndex);
str.substring(int startIndex, int endIndex);

Where, startIndex is inclusive and endIndex is exclusive.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using indexOf() and substring() Method

Let’s see the program along with its output one by one.

Approach: By Using indexOf() and substring() Method

Example

In this approach, by using the above algorithm find the first and last word of the content in a file.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Main {
   public static void main(String[] args) {
      try (BufferedReader br = new BufferedReader(new FileReader("E:\file1.txt"))) {
         String str;

         while ((str = br.readLine()) != null) {
            System.out.println("Content of the txt file:");   
            System.out.println(str);
            int firstSpacePosition = str.indexOf(" ");
            
            //get the first word
            String firstWord = str.substring(0,firstSpacePosition);
            
            //get the last word
            String lastWord = str.substring(str.lastIndexOf(" ")+1);
            
            //print the output of the program
            System.out.println("first word : "+firstWord);
            System.out.println("Last word: "+lastWord);        
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
} 

Output

Content of the txt file:
Java is a well-known high-level, class-based object-oriented programming language that Sun Microsystems created and first distributed in 1995. Over 3 billion devices run Java, which is currently owned by Oracle
first word : Java
Last word: Oracle

In this article, we explored how to find the first and last word of a file containing string in Java.

Updated on: 11-Jan-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements