Results 1 to 3 of 3
Thread: custom class doing wierd things
- 03-11-2013, 08:05 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 59
- Rep Power
- 0
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
- 03-11-2013, 08:44 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 676
- Rep Power
- 1
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.
Regards,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
JimLast edited by jim829; 03-11-2013 at 09:03 PM. Reason: tried to be more specific
The Java™ Tutorial
YAT -- Yet Another Typo
- 03-12-2013, 12:04 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 59
- Rep Power
- 0
Similar Threads
-
Painting things on a JPanel class from another class?
By Alerhau in forum New To JavaReplies: 2Last Post: 10-17-2011, 08:04 PM -
Help with custom class array
By mwr1976 in forum New To JavaReplies: 3Last Post: 10-16-2011, 10:58 PM -
Java custom class, need help!
By Keno777 in forum New To JavaReplies: 12Last Post: 10-12-2009, 02:27 AM -
Importing custom class
By BigRed in forum EclipseReplies: 2Last Post: 02-14-2009, 02:12 PM -
Importing a Custom Class
By jfredrickson in forum New To JavaReplies: 3Last Post: 07-11-2007, 11:23 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks