user3085151
user3085151

Reputation: 3

Identifier Error?

I'm trying to get the count on the number of times "Lenore" is in the poem along with a total word count. I'm getting the error on line 13, please help. I'm very new and can't seem to grasp how to order the code correctly.

package theraven;

import java.io.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class lenore {

    Scanner myscanner = new Scanner("/Users/karaleamann/Desktop/theraven.txt");

    public int countWord(String Lenore, File"/Users/karaleamann/Desktop/theraven.txt") {
        int count = 0;
        while (myscanner.hasNextLine()) {

            String nextToken = myscanner.next();

            if (nextToken.equalsIgnoreCase(Lenore))
                count++;
        }
        return count;
    }

    public int countAll() {
        File file = new File("/Users/karaleamann/Desktop/theraven.txt");
        Scanner sc = null;
        try {
            sc = new Scanner(new FileInputStream(file));

        } catch (FileNotFoundException ex) {

            Logger.getLogger(lenore.class.getName()).log(Level.SEVERE, null, ex);
        }

        int count = 0;
        while (sc.hasNext()) {
            sc.next();
            count++;
        }
        System.out.println("Number of words: " + count);
        return 0;

    }

}

Upvotes: 0

Views: 74

Answers (2)

Jorge Campos
Jorge Campos

Reputation: 23381

First of all you don't have a Main method which is the one needed to execute a class. Second you define the method countWord in an invalid way.

Change your class name to Lenore camelCase pattern, class names should have name with upper case first letter

public int countWord(String Lenore, File "/Users/karaleamann/Desktop/theraven.txt") 

You can't do that. You have to pass just parameters

it would be something like:

public int countWord(String lenore, File file){
                            //^ variables name should be 
                            //      in camelCase
                                        //^you pass a File variable 
                                        //   to the method.

But since you define a scanner on your class you dont need to pass a file to this method, you need to change the definition of your scanner like this:

new Scanner(new File("/Users/karaleamann/Desktop/theraven.txt"));

Then your method countWord should be countWord(String lenora)

You have two methods that are doing nothing. One is using the scanner but it was never called. And the other you are not finding anything at all.

Your countAll method is the closest one to your solution, so lets stick with it.

You should change this part

 while(sc.hasNext()){
     String lineText = sc.next();
     if ( lineText.indexOf("Lenora")>-1 ){
      count++;   
     }
 }

Then create a main method to start your program

public static void main(String [] args){
     Lenore l = new Lenore();
     l.countAll();
}

Off course that is not the ideal code. You have to evolve it to a more logical code. Separating the tasks, creating resources only as needed. But it should work for now.

Upvotes: 2

Óscar López
Óscar López

Reputation: 236150

You can't do this in Java:

public int countWord(String Lenore, File "/Users/karaleamann/Desktop/theraven.txt")

I believe you meant this instead:

public int countWord(String Lenore, File theFile)

The actual value for the parameter theFile must be provided when calling the method, not during parameter declaration, it doesn't work that way.

Upvotes: 0

Related Questions