Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: custom class doing wierd things

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

    Default custom class doing wierd things

    I am testing a custom class, and it seems that no matter what I do, the array only holds the last token in the string. I asked my TA today, and he said that the logic made sense, and that it had to be a compiler error, I just tried it on a different computer and it is still not working. here are the relevant classes, I had an earlier problem with the same thing, but I resolved it.

    Java Code:
    public class sentenceTest
    {
    	public static void main(String[]args)
    	{
    		String test=" banana group g letete dsflajdfio ghithy aklf;asdt akdfjit.";
    		Sentence testS= new Sentence(test);
    		
    		int numWords= testS.countWords();
    		
    		Word firstWord=testS.nextWord();
    		Word secWord=testS.nextWord();
    		Word thWord=testS.nextWord();
    		Word fourWord=testS.nextWord();
    		
    		
    		System.out.println(numWords+"::::"+firstWord.getWord()+"::::"+secWord.getWord()+"::::"+thWord.getWord()+"::::"+fourWord.getWord());
    	}
    }
    Java Code:
    import java.util.StringTokenizer;
    public class Sentence
    {
    	private int numWords=0;
    	private int ct=0;
    	private Word[] sentence;	
    	/** * Short one line description. (1) * * Longer description. If there were any, it would be [2] 
    	* here. * <p> * And even more explanations to follow in consecutive * paragraphs separated by HTML paragraph breaks. 
    	* 
    	* @param variable Description text text text. (3) 
    	* @return Description text text text. 
    	*/ 
    
    	Sentence(String s)
    	{
    		
    		StringTokenizer start=new StringTokenizer(s," ,.");
    		numWords=start.countTokens();
    		sentence = new Word[numWords];
    		
    		int i = 0;
    		while(start.hasMoreTokens())
    		{
    			sentence[i]=new Word(start.nextToken());
    			System.out.println(sentence[i].getWord());
    		
    
    			i++;
    		}
    		
    		for(i = 0; i< numWords; i++)
    			System.out.println(sentence[i].getWord());
    		
    		
    	}
    	/** * Short one line description. (1) * * Longer description. If there were any, it would be [2] 
    	* here. * <p> * And even more explanations to follow in consecutive * paragraphs separated by HTML paragraph breaks. 
    	* 
    	* @param variable Description text text text. (3) 
    	* @return Description text text text. 
    	*/
    	public int countWords()
    	{
    
    
    		return numWords;
    	}
    	/** * Short one line description. (1) * * Longer description. If there were any, it would be [2] 
    	* here. * <p> * And even more explanations to follow in consecutive * paragraphs separated by HTML paragraph breaks. 
    	* 
    	* @param variable Description text text text. (3) 
    	* @return Description text text text. 
    	*/
    	public Word nextWord()
    	{
    		
    	
    		Word returnWord=sentence[ct];
    		if(ct<numWords){
    			ct++;
    			}
    		System.out.println(ct + " --" + sentence[ct].getWord());
    
    		return returnWord;
    	}
    
    
    }
    Java Code:
    public class Word
    {
    	private static final char[]vowels={'a','e','i','o','u','y'};
    	private static int numberOfSyllables;
    	private static char[]word;
    	/** * Short one line description. (1) * * Longer description. If there were any, it would be [2] 
    	* here. * <p> * And even more explanations to follow in consecutive * paragraphs separated by HTML paragraph breaks. 
    	* 
    	* @param variable Description text text text. (3) 
    	* @return Description text text text. 
    	*/	
    	Word(String w)
    	{
    		w=w.toLowerCase();
    		word=w.toCharArray();
    				
    	}
    	/** * Short one line description. (1) * * Longer description. If there were any, it would be [2] 
    	* here. * <p> * And even more explanations to follow in consecutive * paragraphs separated by HTML paragraph breaks. 
    	* 
    	* @param variable Description text text text. (3) 
    	* @return Description text text text. 
    	*/	
    	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;
    		}
    	}
    	/** * Short one line description. (1) * * Longer description. If there were any, it would be [2] 
    	* here. * <p> * And even more explanations to follow in consecutive * paragraphs separated by HTML paragraph breaks. 
    	* 
    	* @param variable Description text text text. (3) 
    	* @return Description text text text. 
    	*/	
    	public static boolean isVowel(char ch)
    	{
    		boolean vowel=false;
    		for(int i=0;i<6;i++)
    		{
    			if(ch==vowels[i])
    			{
    				vowel=true;
    			}
    		}
    	return vowel;
    	}
    	/** * Short one line description. (1) * * Longer description. If there were any, it would be [2] 
    	* here. * <p> * And even more explanations to follow in consecutive * paragraphs separated by HTML paragraph breaks. 
    	* 
    	* @param variable Description text text text. (3) 
    	* @return Description text text text. 
    	*/	
    	public String getWord()
    	{
    		String sWord= new String(word);
    		return sWord;
    	}
    }

    here is the output i get


    banana
    group
    g
    letete
    dsflajdfio
    ghithy
    aklf;asdt
    akdfjit
    akdfjit
    akdfjit
    akdfjit
    akdfjit
    akdfjit
    akdfjit
    akdfjit
    akdfjit
    1 --akdfjit
    2 --akdfjit
    3 --akdfjit
    4 --akdfjit
    8::::akdfjit::::akdfjit::::akdfjit::::akdfjit

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

    Default Re: custom class doing wierd things

    First, compiler errors are very rare. The problem is that your fields in your Word class are declared static.

    Java Code:
      private static final char[]vowels={'a','e','i','o','u','y'}; // static okay here since not part of object state
        private int numberOfSyllables;  // remove static as this should be an instance field
        private char[]word;               // remove static as this should be an instance field
    Regards,
    Jim
    Last edited by jim829; 03-11-2013 at 09:03 PM. Reason: tried to be more specific
    lenois likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: custom class doing wierd things

    Thank you, I figured it out right when you posted I had stared at the sentence class so long, I didn't even look at the word class.

Similar Threads

  1. Replies: 2
    Last Post: 10-17-2011, 08:04 PM
  2. Help with custom class array
    By mwr1976 in forum New To Java
    Replies: 3
    Last Post: 10-16-2011, 10:58 PM
  3. Java custom class, need help!
    By Keno777 in forum New To Java
    Replies: 12
    Last Post: 10-12-2009, 02:27 AM
  4. Importing custom class
    By BigRed in forum Eclipse
    Replies: 2
    Last Post: 02-14-2009, 02:12 PM
  5. Importing a Custom Class
    By jfredrickson in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 11:23 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
  •