Results 1 to 11 of 11
- 03-09-2013, 06:01 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 59
- Rep Power
- 0
New Problem, Using static array, methods not working as expected. see last post
So I am trying to declare an array of a custom class, and am getting a compiler error.
Flesch.java:5: error: class, interface, or enum expected
private static Sentence[]thisFileSentences;
^
Flesch.java:6: error: class, interface, or enum expected
private static Word[]thisFileWords;
^
Here is the driver code
here is the code for the classesJava Code:import java.io.*; import javax.swing.JOptionPane; import java.util.StringTokenizer; public class Flesch { public static Sentence[]thisFileSentences=new Sentence[10000]; public static Word[]thisFileWords= new Word[10000]; public static void main (String[]args)throws IOException { String fileName=getFileName(); String readFile= readTheFile(fileName); int numSentences=countSentences(readFile); System.out.println(numSentences); int numWords=countWords(numSentences); System.out.println(numWords); int numSyllables=countSyllables(numWords); System.out.println(numSyllables); } public static String getFileName() { String fileName=JOptionPane.showInputDialog("What is the name of your file?"); return fileName; } public static String readTheFile(String fileName)throws IOException { String holder; BufferedReader inFile = new BufferedReader( new FileReader(fileName+".txt")); StringBuffer readFile=new StringBuffer(); do { holder=inFile.readLine(); if(holder!=null) { readFile.append(holder+" "); } }while(holder!=null); String sReadFile=readFile.toString(); return sReadFile; } public static int countSentences(String readFile) { StringTokenizer st = new StringTokenizer(readFile,"!.;:"); int i=0; while (st.hasMoreTokens()) { thisFileSentences[i]=new Sentence(st.nextToken()); int x=thisFileSentences[i].countWords(); i++; System.out.println(x); } return i-1; } public static int countWords(int numSentences) { int ct=0; int whileCt; int numWords=0; for (int i=0; i<=numSentences;i++) { whileCt=0; ct=ct+thisFileSentences[i].countWords(); System.out.println(ct); while(whileCt<ct) { thisFileWords[numWords]=thisFileSentences[i].nextWord(); numWords++; whileCt++; } } return numWords; } public static int countSyllables(int numWords) { int numSyllables=0; for(int i=0;i<numWords;i++) { numSyllables=numSyllables+thisFileWords[i].countSyllables(); } return numSyllables; } //public int computeScore() //{ //} }
Java Code:import java.util.StringTokenizer; public class Sentence { private static StringTokenizer sentence=new StringTokenizer(""); public Sentence(String s) { sentence=new StringTokenizer(s," ,"); } public int countWords() { int numWords=sentence.countTokens(); return numWords; } public Word nextWord() { Word nextWord= new Word(sentence.nextToken()); return nextWord; } }I have tested both classes, and they seem fine, but i can't figure out what i did wrong with the driver.Java Code:public class Word { private static final char[]vowels={'a','e','i','o','u','y'}; private static int numberOfSyllables; private static char[]word; Word(String w) { w=w.toLowerCase(); word=w.toCharArray(); } public int countSyllables() { int ct=word.length; for(int i=0;i<ct;i++) { if(isVowel(word[i])) { if(i==ct-1&&word[i]=='e') { } else if(word[i]=='e'&&(word[i+1]=='d'||word[i+1]=='s')) { } else if(i==0) { numberOfSyllables++; }else if(!isVowel(word[i-1])) { numberOfSyllables++; } } } if(numberOfSyllables>1) { return numberOfSyllables; }else { return 1; } } public static boolean isVowel(char ch) { boolean vowel=false; for(int i=0;i<6;i++) { if(ch==vowels[i]) { vowel=true; } } return vowel; } public String getWord() { String sWord= new String(word); return sWord; } }
I mainly want to know if I have to take a different strategy with the driver, which I can easily do.Last edited by lenois; 03-10-2013 at 05:24 PM.
-
Re: compiler error Array of a custom class
Look at the offending lines -- they're in the wrong place. They're outside of any class, and this cannot occur in Java.
- 03-09-2013, 06:07 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 667
- Rep Power
- 1
Re: compiler error Array of a custom class
Your declarations need to be inside the class declaration.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-09-2013, 06:07 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
- 03-09-2013, 06:08 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 667
- Rep Power
- 1
-
- 03-09-2013, 06:10 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
Re: compiler error Array of a custom class
Duh ...
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-09-2013, 08:17 PM #8
Member
- Join Date
- Feb 2012
- Posts
- 59
- Rep Power
- 0
Re: compiler error Array of a custom class
Alright so the compiler error finished now I am having some problem at run time, no errors, just not the results I want.
on line 75
this is always returning 0Java Code:ct=ct+thisFileSentences[i].countWords();
although on line 60
It returns the actual word countJava Code:int x=thisFileSentences[i].countWords();
- 03-09-2013, 08:58 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,397
- Blog Entries
- 7
- Rep Power
- 17
Re: compiler error Array of a custom class
It is time to bring in the poor mens debugger: System.out.println( ...). Sprinkle it in your code here and there and make it print values you're interested in. You'll see some correct results and some stuff you didn't expect. Add some more of those System.out.println( ... ) statements and you have your mistake pinned down before you know it.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-09-2013, 10:50 PM #10
Member
- Join Date
- Feb 2012
- Posts
- 59
- Rep Power
- 0
Re: compiler error Array of a custom class
Java Code:thisFileSentences[i]=new Sentence(st.nextToken()); int x=thisFileSentences[i].countWords(); i++; System.out.println(x);
I know the poor man debugger, that was why I asked. My results were not what I expected.Java Code:whileCt=0; ct=ct+thisFileSentences[i].countWords(); System.out.println(ct);
I am just wondering why the same code doesn't produce the same results when I declared a class variable.Last edited by lenois; 03-09-2013 at 10:59 PM.
- 03-10-2013, 04:52 PM #11
Member
- Join Date
- Feb 2012
- Posts
- 59
- Rep Power
- 0
Similar Threads
-
Class expected error with array
By zlloyd1 in forum New To JavaReplies: 2Last Post: 12-12-2012, 06:26 AM -
Help with custom class array
By mwr1976 in forum New To JavaReplies: 3Last Post: 10-16-2011, 10:58 PM -
Compiler error please help
By ShortIt in forum New To JavaReplies: 5Last Post: 02-15-2011, 11:11 PM -
Custom class in an array?
By bugmenot in forum New To JavaReplies: 5Last Post: 04-15-2009, 12:10 AM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks