
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Capitalize last letter and Lowercase first letter of a word in Java
- Find the first repeated word in a string in Java
- Python - Find the length of the last word in a string
- Find the first repeated word in a string in Python?
- Find the first repeated word in a string in C++
- Getting first letter of each word in a String using regex in Java
- Find frequency of each word in a string in Java
- Java Program to Capitalize the first character of each word in a String
- How to print the first character of each word in a String in Java?
- Find the first repeated word in a string in Python using Dictionary
- PHP program to find the length of the last word in the string
- Find the first maximum length even word from a string in C++
- Print first letter of each word in a string in C#
- Delete first and last element from a LinkedList in Java
- How to match a line not containing a word in Java Regex
