Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can anybody tell me, what I have done wrongly here? I can't know what the problem is

I have written a code for a program that counts, how many words in a given file. The problem is, I can’t play the program. Can anybody help! By the way, I’m a beginner at programming.
Here is my code:

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

public class TextAnalysis22 {

  public static void main(String[] args) 
          throws FileNotFoundException {
  System.out.println("Enter the path of the file");
      Scanner console = new Scanner (System.in);
      fileName(console);
  

  }

  public static Scanner fileName (Scanner console)
  throws FileNotFoundException{
      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);    
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);
  }

}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Not sure why you are using recursion at Scanner data = fileName(console);
and also the while loop is supposed to be after you read the data from the file.

you need to update the sequence of the things you are trying to do.
Step1. Take the file name as input from the console using the scanner in the main method.

Scanner sc= new Scanner(System.in); //System.in is a standard input stream  
System.out.print("Enter a name: ");  
String str= sc.nextLine();              //reads string   
System.out.print("file name: "+str);   

Step 2: read the file. (you don’t need to pass the scanner in the fileName method also)

BufferedReader reader = new BufferedReader(new FileReader(fileName));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
    stringBuilder.append(line);
    stringBuilder.append(ls);
}
// delete the last new line separator
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
reader.close();

String content = stringBuilder.toString();

step 3: use logic to count the chars. (I don’t think you need just print the length of string content).

you won’t be needing recursion to do so.

hope this helps.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading