I’m creating a Java program that can load different text documents (in this case, the first chapter or so of four different novels) and then print them out in order of their vocabulary ranks. In other words, they are ranked based on how many times words are repeated in the chapter. This program includes three classes:
hw5.java:
public class hw5 { //main method public static void main(String[] args) throws FileNotFoundException { //stores user entered data String userInput = ""; //Scanner class object created Scanner in = new Scanner(System.in); System.out.println("Write a file name to include in your ranks, '?' to list ranks, and ! to exit program"); //Accepts user choice and loops while user choice is not "!" while(!userInput.equals("!")) { System.out.print(">> "); userInput = in.next(); //if user choice is "!", the program stops if (userInput.equals("!")) { System.out.println("nGood Bye :)"); } //if user choice is "?", the program stops else if(userInput.equals("?")) { //calls printRanks() method from StoryRanker to display file name and word count StoryRanker.printRanks(); } //Otherwise it's a file name else { //Uses default constructor to pass the file name to Story class Story myStory = new Story(userInput); //if getWordCount method from Story is greater than 0, call addStory() method from StoryRanker and pass the object if(myStory.getWordCount()>0) { StoryRanker.addStory(myStory); } } } } }
Story.java:
public class Story { //stores word count int wordCount; //stores file name String title; //Scanner class object for reading file Scanner fileRead; //file object File file; //Constructor for receiving file name Story(String fileName){ //initializes file object by file name file = new File(fileName); //extracts file extension title = fileExtension(fileName); } //Method that returns number of words in the file int getWordCount() { //try block try { //file is read fileRead = new Scanner(file); //Checks for data while(fileRead.hasNextLine()) { //Word is read fileRead.next(); //wordCount increases wordCount++; } //File is closed fileRead.close(); } //Catch for FileNotFoundException catch (FileNotFoundException e) { System.out.println("File Not Found"); } //returns wordCount return wordCount; } //method that returns file name from complete file name with extension static String fileExtension(String str) { //null case if(str == null) { return null; } //gets position of last "." int pos = str.lastIndexOf("."); //if there's no "." return the string as it is if(pos == -1) { return str; } //returns string up to "." return str.substring(0,pos); } }
and StoryRanker.java:
class Data { //Stores file name String fileName; //stores word count int wordCount; } class MyNewStack{ //stores 100 file names and word count Data myData[] = new Data[100]; //points to top int top; //stores length int length; //default constructor void MyNewStack() { top = -1; length = 0; } //Returns false if top = -1, but otherwise returns true boolean isEmpty() { if(top == -1) { return false; } else { return true; } } //method pushes object to stack void push (Data d) { //Checks is stack is full if (top == 99) { System.out.println("Stack is full"); } //if stack isn't full else { //increases top position of stack by one ++top; //instantiates the array top index position myData[top] = new Data(); //Stores file name and word count myData[top].fileName = d.fileName; myData[top].wordCount = d.wordCount; //increases length by one length++; } } //method to pop the stack top position Data pop() { //if stack top is less than zero, the stack is empty if(top<0) { System.out.println("Stack Underflow."); return null; } //Otherwise, decrease length by one and return stack top position object else { length--; return myData[top--]; } } //method to return stack a position Data peek(int a) { //if stack top is less than zero, the stack is empty if(top < 0) { System.out.println("Stack underflow."); return null; } //otherwise returns stack a position object else { return myData[a]; } } } //establish StoryRanker public class public class StoryRanker { //MyNewStack class objects static MyNewStack stack1 = new MyNewStack(); static MyNewStack stack2 = new MyNewStack(); //Data class object static Data d = new Data(); //Method to add file name and word count to stack public static void addStory(Story myStory) { //Extract data from myStory and store it in Data object d.fileName = myStory.title; d.wordCount = myStory.wordCount; //checks if stack is not empty if(!stack1.isEmpty()) { //Push object to first stack stack1.push(d); } else { //loops until first stack top position for(int a = 0; a <= stack1.top; a++) { //if stack word count is less than current file word count if(stack1.peek(a).wordCount < myStory.wordCount) { //Push object to second stack stack2.push(stack1.pop()); } } //puush current object to first stack stack1.push(d); //for loop until second stack top position for(int a = 0; a < stack2.length; a++) { //extracts second stack object and pushes it to first stack stack1.push(stack2.pop()); } } } //Method to display first stack contents public static void printRanks() { //loops until first stack top position for(int a = 0; a <= stack1.top; a++) { System.out.println((a+1)+ ": " + stack1.peek(a).fileName + ", size = " + stack1.peek(a).wordCount); } } }
And here is what it should look like when running the program:
Write a file name to include your ranks, '?' to list ranks, and ! to exit program >>the_boy_who_lived.txt >>? 1: the_boy_who_lived, size= 392. >>dracula_chapter1.txt >>? 1: dracula_chapter1, size= 1309. 2: the_boy_who_lived, size= 392. >>! Good Bye :)
The problem I’m running into is that whenever I enter one of the files I am using, it keeps returning “File Not Found.” I’ve tried typing in the entire address, including the .txt at the end, and nothing seems to be going through. Is there anyone who can help me?
Advertisement
Answer
Story(String fileName){ //initializes file object by file name String directory = "C:\Users\stlp\IdeaProjects\StoryCount\src.\" + fileName + ".txt"; file = new File(directory); //extracts file extension title = fileExtension(fileName); }
Replace the first string of directory with your actual absolute directory. Make sure to include the double slashes. When inputting the file name, you want to put in just the name as the .txt is appended already.