Why am I getting an "Expected Identifier" error?

Java "error: class, interface or enum expected"?

  • heres my code. its supposed to ask for a sentence and a minimum amount of letters for a word and count the amount of words. but I get 21 errors and they're all the same thing: "error: class, interface or enum expected." What does this mean and how do I fix this? all my syntax is fine otherwise --------------------------------------… public static boolean fourUp (String currentWord, int min){ //this method will tell wordcount whether to increment int letterCounter=0; char currentChar; boolean fourUp=false; String currentWord=currentWord.trim; for(int i=0; i<=currentWord.length(); i++){ currentChar=currentWord.charAt(i); if(currentChar.isLetter) letterCounter++; } if(letterCounter>=min) fourUp=true; else fourUp=false; return fourUp; } public static String spaceFinder (int letterPosition, String str){ //this method will find the spaces String currentWord; int strLength=str.length(); for(int i=0; i<strLength; i++){ if(str.charAt(i)==' '){ currentWord=str.substring(letterPo… i); } } if(str.charAt(i)==strLength){ currentWord=str.substring(letterPosit… str.length(); currentWord=currentWord.trim; } return currentWord; } public class WordCount{ public static void main (String [] args){ /*Well, I had a main method where a person enters a string and another method that takes the string and finds the first space. I then set up another string that takes the part of the first string up until that space and check each letter in the loop to see if it is a letter. If it is greater than the entered amount, i count++ the word. I then cut the first word checked from the old string and do this all in a loop. Once there are no more spaces and the string is empty I return the total count. */ int min; String str; int wordCount=0; int letterPosition=0; boolean fourUp; System.out.println("enter sentence: "); //just getting input str=IO.readString(); //jus… getting input System.out.println("enter minimum letters: "); //just getting input min= IO.readInt(); //just getting input while(min<0){ //just getting input IO.reportBadInput(); //jus… getting input min=IO.readInt(); //just getting input } do{ currentWord=WordCount.spaceFinder(le… str); fourUp=WordCount.fourUp(currentWord… if(fourUp==true) wordCount++; fourUp=false; letterPosition=currentWord.length(); }while(letterPosition<=str.length() ); IO.outputIntAnswer(wordCount); } } --------------------------------------… 10 points to best answer. Thanks, Do21or12Die

  • Answer:

    It appears that the first line of your code is a method and that needs to be a "class, interface or enum". I didn't take too much time to understand the entire code, but if the entire thing is suppose to be 1 class you'll need to move public class WordCount{ before the first method is declared. PS: After trying to compile after this, there was an error of currentWord already being declared on the line String currentWord=currentWord.trim;

TnT at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.