Click here to Skip to main content
15,908,274 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've made a program that counts, how many words in a text file. And it counts the accurance of a given word in the given text file. The first method that counts the words in the text file is working completely fine, but when the program reaches the method number 2 (frequency) it craches. Can anybody tell me, what I have done wrongly.

When I play the program and it reaches the second method, I get this error "in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:941)
at java.base/java.util.Scanner.next(Scanner.java:1482)
at TextAnalysis22.main(TextAnalysis22.java:19)"

Thanks in advance!

What I have tried:

Java
import java.io.*;
import java.util.*;

public class TextAnalysis22 {

	public static void main(String[] args) 
			throws FileNotFoundException {
		Scanner console = new Scanner (System.in);
		Scanner data = fileName(console);
		int count = 0;
		while(data.hasNext()) {
			String word = data.next();
			count++;
		}
		System.out.println("The number of words in the given file is " + count);
		
	frequency(data.next(),console.next());		
	}
	
	public static Scanner fileName (Scanner console)
	throws FileNotFoundException{
System.out.println("Enter the path of the file to count the words");
File input = new File(console.nextLine());
while(!input.canRead()) {
	System.out.println("The given file is not found. Try again.");
	System.out.println("Enter the path of the file");
	 input = new File(console.nextLine());
}
	return new Scanner(input);
	}

public static int frequency(String a,String s) {
	  System.out.println("Enter the word to be Searched");
	  int counter = 0;
	  String [] words = null;
	  while(a!=null) {
		  words = a.split(" ");
		  for (String word : words) {
			  if(word.equals(s)) {
				  counter ++;
				  System.out.println("The given word is present for "+counter+ " Times in the file");
			  }
		  }
	  }
	return counter;
	  }
}
Posted
Updated 10-Oct-22 4:04am
Comments
Member 8428760 10-Oct-22 18:52pm    
frequency(data.next(),console.next()); data.next() has nothing at this point.

1 solution

Probably because you only call frequency once HasNext returns false - and then you try to call next to get another element on a stream you know has nothing to get ...

I'd strongly suggest that You go back to eth original assignment and think about what you are supposed to be doing: unless you store your words as they come in, you will have to process the file multiple times to do what you appear to be attempting.
 
Share this answer
 
Comments
ShadyGhabour 10-Oct-22 13:10pm    
@OrginalGriff
The assignment say the following, so I don’t know, if I do it correctly ☹️
“ Problem 4 [35%]: [Text anlysis] We want to write a program which allows to analyse texts.
Write a class TextAnalysis22 which reads a text file and allows some text mining. Concretely the program has to support
• the number of words.
• checking how often a word is contained in the text. This is case-sensitive.
public int wordCount() // returns the number of words in the file public int frequency(String word) // returns the number of occurrences
// of "word" in the text (case-sensitive).
How would u solve it?
OriginalGriff 10-Oct-22 13:16pm    
I'd follow the - pretty explicit - instructions: create a class, give it a constructor which reads a file into words that you store (hint: does Java have a Dictionary class?)
Then add a method that returns the frequency of a given word.
Then add a method that returns the number of words (I'd probably have two: one for unique words, one for total words).

Try it on paper manually - you'll get ethe idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900