For example I have a text file that contains the contents of each line of a book, I have a java program to search for a particular word in those lines from the book.
This is the program:
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class AliceSearch {
public static void main(String[] args) throws Exception {
ArrayList<String> aiw = new ArrayList<String>();
ArrayList<String> matches = new ArrayList<String>();
Scanner scan = new Scanner(new File("aiw.txt"));
Scanner input = new Scanner(System.in);
while (scan.hasNext()){
aiw.add(scan.nextLine());
}
String searchTerm;
System.out.print("Please Input Search Parameter : ");
searchTerm = input.nextLine();
boolean itemFound = false;
String currItem = null;
for(int i = 0; i<aiw.size(); i++ ) {
currItem = (String)aiw.get(i);
if (currItem.contains(searchTerm)) {
matches.add(currItem);
itemFound = true;
}
}
System.out.println("");
if ( itemFound == false ) {
System.out.println ( "No results containing "+searchTerm );
}else{
System.out.println ( "We Found the following results : " );
for(int r = 0; r < matches.size(); r++){
System.out.println("");
System.out.println(matches.get(r));
}
}
scan.close();
input.close();
}
}
I would like the searchTerm from each resultant line to be in uppercase when outputed (or when placed in the matches ArrayList). How would i go about this? I know that you use .toUpperCase(); but I do not now how i can change one word in a string of words.
Thanks in advance!
replaceis the right choice.replaceAllinvolves regular expressions, which OP doesn't need.