当前位置: 动力学知识库 > 问答 > 编程问答 >

arrays - Search for a word in a file results in java.util.NoSuchElementException

问题描述:

This is my code. It produces the error java.util.NoSuchElementException.

It is meant to search a file, example.txt for a word (eg. and) and find all instances of the the word and print the word either side of it also (eg. cheese and ham, tom and jerry) in ONE JOptionPane. Code:

import java.io.File;

import java.util.Arrays;

import java.util.Scanner;

import javax.swing.JOptionPane;

public class openFileSearchWord {

public static void main(String Args[])

{

int i=0,j=0;

String searchWord = JOptionPane.showInputDialog("What Word Do You Want To Search For?");

File file = new File("example.txt");

try

{

Scanner fileScanner = new Scanner(file);

String[] array = new String[5];

String[] input = new String[1000];

while (fileScanner.hasNextLine())

{

for(i=0;i<1000;i++)

{

input[i] = fileScanner.next();

if(input[i].equalsIgnoreCase(searchWord))

{

array[j] = input[i-1] + input[i] + input[i+1];

j++;

}

}

}

Arrays.toString(array);

JOptionPane.showMessageDialog(null, array);

fileScanner.close();

}

catch(Exception e)

{

System.out.println(e);

}

}

}

网友答案:

It looks like you're assuming each line will have 1000 words.

  while (fileScanner.hasNextLine())
  {
      for(i=0;i<1000;i++) <-------- Hardcoded limit?
      {
          ....
      }
  }

You can try putting another catch loop, or check hasNext() during that for loop.

while (fileScanner.hasNextLine())
{
    for(i=0;i<1000 && fileScanner.hasNext();i++)
    {
        ....
    }
}

There are also many issues with your code, like if input[i-1] hits the -1 index, or if your 'array' array hits the limit.

I took the liberty to have some fun.

Scanner fileScanner = new Scanner(file);    
List<String> array = new ArrayList<String>();

String previous, current, next;

while (fileScanner.hasNext())
{
    next = fileScanner.next());  // Get the next word
    if(current.equalsIgnoreCase(searchWord))
    {
        array.add( previous + current + next );
    }

    // Shift stuff
    previous = current;
    current = next;
    next = "";
}

fileScanner.close();

// Edge case check - if the last word was the keyword
if(current.equalsIgnoreCase(searchWord))
{
    array.add( previous + current );
}

// Do whatever with array
....
网友答案:

I see a few error here ...

You are creating two arrays one with 5 and one with 1000 elements. In your code you are referencing elements directly by index ... but this index might not be present.

input[i-1] ... what if i = 0? ...index is -1 

array[j] ... what if j > 4 ... index 5 doesn't exist

I suggest using List of elements instead of fixed arrays.

List<String> array = new ArrayList<>();

You are assuming that the input is something but don't do anything to check what it actually is.

网友答案:

Just as Drejc told you, The first iteration would fail because of the negative index and the program will fail as well if it finds more than 5 matches of the desired word.

Also I want to add another one. You should think that when you do this line:

array[j] = input[i-1] + input[i] + input[i+1];

You have not assigned input[i+1] yet. In that iteration you've just assigned input[i], but no the next one.

You should process the concatenation of the three elements (previousWord + match + nextWord) when reaching nextWord.

Another solution, but inefficient, would be copying all the words to an Array at beginning and using your actual code without modifying. This would work, but you would go twice through all the words.

分享给朋友:
您可能感兴趣的文章:
随机阅读: