Results 1 to 11 of 11
  1. #1
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default 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
    Java 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()
    	//{
    	//}
    }
    here is the code for the classes

    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;
    	}
    
    
    }
    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 have tested both classes, and they seem fine, but i can't figure out what i did wrong with the driver.

    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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: compiler error Array of a custom class

    Your declarations need to be inside the class declaration.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,373
    Blog Entries
    7
    Rep Power
    20

    Default Re: compiler error Array of a custom class

    Quote Originally Posted by lenois View Post
    Java Code:
    import java.io.*;
    import javax.swing.JOptionPane;
    import java.util.StringTokenizer;
    
    		private static Sentence[]thisFileSentences;
    		private static Word[]thisFileWords;
    public class Flesch
    { ... }
    What are those two static variable definitions doing there outside of any class?

    kind regards,

    Jos

    ps. and who's the slowest old sod again?
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,416
    Rep Power
    5

    Default Re: compiler error Array of a custom class

    Hah!
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: compiler error Array of a custom class

    Quote Originally Posted by jim829 View Post
    Hah!
    Hah right back atcha!

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,373
    Blog Entries
    7
    Rep Power
    20

    Default Re: compiler error Array of a custom class

    Duh ...
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default 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

    Java Code:
    ct=ct+thisFileSentences[i].countWords();
    this is always returning 0

    although on line 60

    Java Code:
    int x=thisFileSentences[i].countWords();
    It returns the actual word count

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,373
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default 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);
    Java Code:
     whileCt=0;   
    ct=ct+thisFileSentences[i].countWords();  
    System.out.println(ct);
    I know the poor man debugger, that was why I asked. My results were not what I expected.

    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.

  11. #11
    lenois is offline Member
    Join Date
    Feb 2012
    Posts
    59
    Rep Power
    0

    Default Re: compiler error Array of a custom class

    So no matter where I put the second iteration of the count words method it only works correctly where I first assigned sentences to it.

    Why is it that the class variable array is changing between methods?

Similar Threads

  1. Class expected error with array
    By zlloyd1 in forum New To Java
    Replies: 2
    Last Post: 12-12-2012, 06:26 AM
  2. Help with custom class array
    By mwr1976 in forum New To Java
    Replies: 3
    Last Post: 10-16-2011, 10:58 PM
  3. Compiler error please help
    By ShortIt in forum New To Java
    Replies: 5
    Last Post: 02-15-2011, 11:11 PM
  4. Custom class in an array?
    By bugmenot in forum New To Java
    Replies: 5
    Last Post: 04-15-2009, 12:10 AM
  5. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •