user6384481
user6384481

Reputation:

how to find i of a token in an array[i]

So, I've found a word in a document and print the line in which the word is present like this:

say example file contains : "The quick brown fox jumps over the lazy dog.Jackdaws love my big sphinx of quartz."

 FileInputStream fstream = new FileInputStream(file);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;        
        //Read File Line By Line
        while((strLine = br.readLine()) != null){                
            //check to see whether testWord occurs at least once in the line of text
            check = strLine.toLowerCase().contains(testWord.toLowerCase());

            if(check){                    
                //get the line, and parse its words into a String array
                String[] lineWords = strLine.split("\\s+"); 

                for(int i=0;i<lineWords.length;i++){
                    System.out.print(lineWords[i]+ ' ');

                }

And if I search for 'fox' , then linewords[] will contain tokens from the first sentence. and linewords[3] = fox. To print the color of the fox, I need linewords[2]. I was wondering how can we get the 'i' of a token in that linewords[i], because I want the output to be linewords[i-1]

Upvotes: 2

Views: 517

Answers (3)

joel314
joel314

Reputation: 1080

If you are using Java 8, it is straightforward:

    List<String> words =  Files.lines(Paths.get("files/input.txt"))
         .flatMap(line -> Arrays.stream(line.split("\\s+")))
         .collect(Collectors.toList());

    int index = words.indexOf("fox"); 
    System.out.println(index);

    if(index>0)
        System.out.println(words.get(index-1));

This solution works also when the word you are searching is the first words in a line. I hope it helps!

If you need to find all occurences, you can use the indexOfAll method from this post.

Upvotes: 2

wake-0
wake-0

Reputation: 3966

You could use a hashMap which stores the word and a list with the indices.

HashMap<String, List<Integer>> indices = new HashMap<>();

So in the for loop you fill the HashMap:

  for(int i=0;i<lineWords.length;i++){
       String word = lineWords[i];
       if (!indices.contains(word)) {
          indices.put(word, new ArrayList<>();
       } 
       indices.get(word).add(i);
  }

To get all the indices of a specific word call:

  List<Integer> indicesForWord = indices.get("fox");

And to get the i - 1 word call:

  for (int i = 0; i < indicesForWord.size(); i++) {
      int index = indicesForWord[i] - 1;
      if (index >= 0 || index >= lineWords.length) {
          System.out.println(lineWords[index]);
      }
  }

Upvotes: 2

yash sachdeva
yash sachdeva

Reputation: 636

That can be done by traversing the array and when you get your word , print the one before it.Here's how:-

if(lineWords[0].equals(testWord)
return;//no preceding word

   for(int i=1;i<lineWords.length;i++){
    if(lineWords[i].equals(testWord){
    System.out.println(lineWords[i-1]);
    break;
    }
   }

Upvotes: 1

Related Questions