1

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!

6
  • 1
    Can you use String.replaceAll method like myString.replaceAll("abc", "ABC") Commented Jan 14, 2013 at 14:07
  • 2
    No, replace is the right choice. replaceAll involves regular expressions, which OP doesn't need. Commented Jan 14, 2013 at 14:11
  • Do not you need to capitalize "City" as well as "city"? Commented Jan 14, 2013 at 14:14
  • @AudriusMeškauskas I am not sure what you mean.. Commented Jan 14, 2013 at 14:18
  • 1
    He means, you want to replace both "city" and "City" with "CITY". Commented Jan 14, 2013 at 14:19

3 Answers 3

2

Instead of outputting it the way you do it right now:

System.out.println(matches.get(r));

can't you use

System.out.println(matches.get(r).replace(searchTerm, searchTerm.toUpperCase()));

Here is the JavaDoc for the replace() method used to replace the found word with it's uppercase version. It would be better to have

String uppercase = searchTerm.toUpperCase();

outside of the loop and then use

System.out.println(matches.get(r).replace(searchTerm, uppercase));
Sign up to request clarification or add additional context in comments.

Comments

1

There is a method on String that should fit your use case exactly:

line.replace(word, word.toUpperCase());

Comments

0

This can be easily done using replace functionality of the pattern. We surely also need to highlight the partially capitalized words (start of the sentence, for instance), so need to create a Pattern with flags, cannot just use String.replaceAll():

 Pattern highlight = Pattern.compile(
   Pattern.quote(word), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE );
 String hw = word.toUpperCase();
 line = highlight.matcher(line).replaceAll(hw);

The first two lines should be prepared in advance as soon as the word is known. There is no need to recompute them newly for every found line. Pattern.quote quotes reserved characters so they will be given no special meaning.

3 Comments

This is overly verbose. Why not simply "(?i)word". For extra safety, run word through Pattern.quote.
Yes, this way is also possible.
Pattern.quote makes a lot of sense

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.