| | |
Word Counter Help
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2006
Posts: 1
Reputation:
Solved Threads: 0
I need help working on my Java project. Any amount is welcome.
The project needs to prompt the user for a file input (txt), file output, and five words to search through the input file. Each input needs to be equipped with try and catch code to prevent crashing. The program then counts each instance of the word in the input file and outputs the results in the designated output file entered by the user.
I'm currently just working on the file input portion and trying to prompt the user to "Enter input file" even after the first attempt was caught by the catch FileNotFound Exception.
Here's my current code.
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
public class WordCounter{
public static void readFile (){
String word = "";
System.out.println("Enter input file:");
Scanner scanner = new Scanner (System.in);
File file = new File(scanner.next());
String line = "";
try{
scanner = new Scanner (file);
}
catch(FileNotFoundException exception){
System.out.println(exception);
System.out.println("Not a valid file");
System.out.println("Enter input file:");
}
while (scanner.hasNextLine()){
line = scanner.nextLine ();
}
System.exit(0);
scanner.close();
}
The project needs to prompt the user for a file input (txt), file output, and five words to search through the input file. Each input needs to be equipped with try and catch code to prevent crashing. The program then counts each instance of the word in the input file and outputs the results in the designated output file entered by the user.
I'm currently just working on the file input portion and trying to prompt the user to "Enter input file" even after the first attempt was caught by the catch FileNotFound Exception.
Here's my current code.
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
public class WordCounter{
public static void readFile (){
String word = "";
System.out.println("Enter input file:");
Scanner scanner = new Scanner (System.in);
File file = new File(scanner.next());
String line = "";
try{
scanner = new Scanner (file);
}
catch(FileNotFoundException exception){
System.out.println(exception);
System.out.println("Not a valid file");
System.out.println("Enter input file:");
}
while (scanner.hasNextLine()){
line = scanner.nextLine ();
}
System.exit(0);
scanner.close();
}
•
•
Join Date: Mar 2004
Posts: 786
Reputation:
Solved Threads: 39
For getting a file, you could take a look at JFileChooser.
Java Syntax (Toggle Plain Text)
String input = "a line from the file"; String word = "key word to search for"; int occurrences = 0; int index = 0; while ((index = input.indexOf(word,index)) > -1 && index < input.length()) { occurrences++; index += word.length(); } System.out.println(occurrences);
![]() |
Similar Threads
- word counter, frequency, percentage (Java)
- word counter (C++)
- letter and word counter (C)
- need help on word counter (C++)
Other Threads in the Java Forum
- Previous Thread: I Need Help With A Project
- Next Thread: Still a problem
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows





