Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Diargg

Thread: Need help with a wordgenerator

  1. #1
    Dowie is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Need help with a wordgenerator

    So, I'm just abit more then a week into learning java and have been reading through the "head first Java"- book the whole time, and just decided i wanted to try program something of my own, and decided to make a word-generator. It's a program that will randomly pick letters to make a word and make sure the word won't have two consonants or vocals in a row, unless it's two of thesame letter. My problem is that I'm trying to figure out how to make sure there won't be three consecutive letters in the word, but i just can't figure it out, maybe you guys/gals could help me?

    Oh, and please give me some critique on the code and some suggestions how to make the code better/more efficient. And sorry for my mix between English and Swedish in the code :)


    Java Code:
     import java.util.*;
    public class WordGen {
    	LetterGenerator GB = new LetterGenerator(); 
    	
    	public void PrintWord() {
    		System.out.print(GenerateWord(wordLength()) + " ");
    	}
    	
    	public int wordLength() {
    		int x;
    		int y = (int) (Math.random() *1000);
    		if (y < 200) {
    			x = (int) (Math.random() * 11 + 2);
    				} else {
    					x = (int) (Math.random()*5 + 3);
    				}
    		return x;
    	}
    			
    	public String GenerateWord(int length) {
    		String word = "";
    		String check1, check2 = null;
    		ArrayList<String> theWord = new ArrayList<String>();
    		check1 = GB.generateLarge();
    		word = word + check1;
    		
    		for (int i = 1; i < length; i++) {
    			check2 = GB.generateSmall();
    			if (isKonsonant(check1) == true && isKonsonant(check2) == false||
    				isKonsonant(check1) == false && isKonsonant(check2) == true||
    				check1 == check2||check1=="c" && check2=="k"||check1=="C" && check2=="K") {
    				theWord.add(check2);
    				} else {
    					i--;
    				}
    			check1 = check2;
    		}
    				
    		for (int i = 0;i < theWord.size(); i++) {
    			word = word + theWord.get(i);
    		}
    		
    		return word;
    	}
    		
    	public boolean isKonsonant(String s) {
    		if (s=="B"||s=="C"||s=="D"||s=="F"||s=="G"||s=="H"||s=="J"||
    			s=="K"||s=="L"||s=="M"||s=="N"||s=="P"||s=="Q"||s=="R"||
    			s=="S"||s=="T"||s=="V"||s=="W"||s=="Z"||s=="X"||s=="b"||
    			s=="c"||s=="d"||s=="f"||s=="g"||s=="h"||s=="j"||s=="k"||
    			s=="l"||s=="m"||s=="n"||s=="p"||s=="q"||s=="r"||s=="s"||
    			s=="t"||s=="v"||s=="w"||s=="z"||s=="x") {
    			return true;
    				} else {
    					return false;
    				}	
    			
    	}
    }
    
    
    
    public class LetterGenerator {
    	
    	public String generateLarge() {
    		String letter = null;
    		int randomNum = (int) (Math.random() * 28);
    		
    		if (randomNum == 0) {
    			letter = "A";
    		} else if (randomNum == 1) {
    			letter = "B";
    		} else if (randomNum == 2) {
    			letter = "C";
    		} else if (randomNum == 3) {
    			letter = "D";
    		} else if (randomNum == 4) {
    			letter = "E";
    		} else if (randomNum == 5) {
    			letter = "F";
    		} else if (randomNum == 6) {
    			letter = "G";
    		} else if (randomNum == 7) {
    			letter = "H";
    		} else if (randomNum == 8) {
    			letter = "I";
    		} else if (randomNum == 9) {
    			letter = "J";
    		} else if (randomNum == 10) {
    			letter = "K";
    		} else if (randomNum == 11) {
    			letter = "L";
    		} else if (randomNum == 12) {
    			letter = "M";
    		} else if (randomNum == 13) {
    			letter = "N";
    		} else if (randomNum == 14) {
    			letter = "O";
    		} else if (randomNum == 15) {
    			letter = "P";
    		} else if (randomNum == 16) {
    			letter = "Q";
    		} else if (randomNum == 17) {
    			letter = "R";
    		} else if (randomNum == 18) {
    			letter = "S";
    		} else if (randomNum == 19) {
    			letter = "T";
    		} else if (randomNum == 20) {
    			letter = "U";
    		} else if (randomNum == 21) {
    			letter = "V";
    		} else if (randomNum == 22) {
    			letter = "W";
    		} else if (randomNum == 23) {
    			letter = "X";
    		} else if (randomNum == 24) {
    			letter = "Y";
    		} else if (randomNum == 25) {
    			letter = "Z";
    		} else if (randomNum == 26) {
    			letter = "";
    		} else if (randomNum == 27) {
    			letter = "";
    		} else if (randomNum == 28) {
    			letter = "";
    		} return letter;
    	}
    	
    	public String generateSmall() {
    		String letter = null;
    		int randomNum = (int) (Math.random() * 28);
    		
    		if (randomNum == 0) {
    			letter = "a";
    		} else if (randomNum == 1) {
    			letter = "b";
    		} else if (randomNum == 2) {
    			letter = "c";
    		} else if (randomNum == 3) {
    			letter = "d";
    		} else if (randomNum == 4) {
    			letter = "e";
    		} else if (randomNum == 5) {
    			letter = "f";
    		} else if (randomNum == 6) {
    			letter = "g";
    		} else if (randomNum == 7) {
    			letter = "h";
    		} else if (randomNum == 8) {
    			letter = "i";
    		} else if (randomNum == 9) {
    			letter = "j";
    		} else if (randomNum == 10) {
    			letter = "k";
    		} else if (randomNum == 11) {
    			letter = "l";
    		} else if (randomNum == 12) {
    			letter = "m";
    		} else if (randomNum == 13) {
    			letter = "n";
    		} else if (randomNum == 14) {
    			letter = "o";
    		} else if (randomNum == 15) {
    			letter = "p";
    		} else if (randomNum == 16) {
    			letter = "q";
    		} else if (randomNum == 17) {
    			letter = "r";
    		} else if (randomNum == 18) {
    			letter = "s";
    		} else if (randomNum == 19) {
    			letter = "t";
    		} else if (randomNum == 20) {
    			letter = "u";
    		} else if (randomNum == 21) {
    			letter = "v";
    		} else if (randomNum == 22) {
    			letter = "w";
    		} else if (randomNum == 23) {
    			letter = "x";
    		} else if (randomNum == 24) {
    			letter = "y";
    		} else if (randomNum == 25) {
    			letter = "z";
    		} else if (randomNum == 26) {
    			letter = "";
    		} else if (randomNum == 27) {
    			letter = "";
    		} else if (randomNum == 28) {
    			letter = "";
    		} return letter;
    	}
    }
    
    
    
    public class WGRun {
    	public static void main (String[] args) {
    		WordGen w2 = new WordGen();
    		w2.PrintWord();
    	}
    }
    Last edited by Dowie; 05-03-2012 at 01:48 AM.

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Need help with a wordgenerator

    Arrays. Use them. They will make your life easier. String[] alphabet = {"a","b", "c","d"...}
    This will let you change generateSmall() to just return alphabet[randomNum];

    s=="G" will always return false - Java requires the .equals() notation, not ==. However, if you create an array of Consonants, you can just return consonants.contains(s).
    (Doing if(consonants.contains(s))return true;else return is needlessly verbose - just return the boolean returned by .contains)

    Also, generateLarge = generateSmall().toUpperCase().
    Fubarable likes this.

  3. #3
    Dowie is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Need help with a wordgenerator

    Quote Originally Posted by Diargg View Post
    Arrays. Use them. They will make your life easier. String[] alphabet = {"a","b", "c","d"...}
    This will let you change generateSmall() to just return alphabet[randomNum];

    s=="G" will always return false - Java requires the .equals() notation, not ==. However, if you create an array of Consonants, you can just return consonants.contains(s).
    (Doing if(consonants.contains(s))return true;else return is needlessly verbose - just return the boolean returned by .contains)

    Also, generateLarge = generateSmall().toUpperCase().
    Many many thanks for the help mate. Been making some changes now, does this look better?

    Java Code:
    public class WGRun {
    	
    	public static void main (String[] args) {
    		WordGen w2 = new WordGen();
    		w2.print(2);
    		
    	}
    }
    
    
    import java.util.*;
    public class WordGen {
    	
    	LetterGenerator GB = new LetterGenerator(); 
    	
    	public void print(int length) {
    		for (int y = 0; y < length; y++) {
    			int r = (int) (Math.random() *20);
    				for (int i = 0 ; i < r; i++) {
    					System.out.print(GenerateWord(wordLength()) + " ");
    				}
    			System.out.print(GenerateWord(wordLength()) + ". ");
    		}
    	}
    	
    	public int wordLength() {
    		int x;
    		int y = (int) (Math.random() *10);
    		if (y < 2) {
    			x = (int) (Math.random() * 11 + 2);
    				} else {
    					x = (int) (Math.random()*5 + 3);
    				}
    		return x;
    	}
    			
    	public String GenerateWord(int length) {
    		String word = "";
    		String check1, check2 = null;
    		ArrayList<String> theWord = new ArrayList<String>();
    		check1 = GB.generateLetter();
    		word = word + check1.toUpperCase();
    		
    		for (int i = 1; i < length; i++) {
    			check2 = GB.generateLetter();
    			if (GB.isVokal(check1) == true && GB.isVokal(check2) == false||
    				GB.isVokal(check1) == false && GB.isVokal(check2) == true||
    				check1 == check2||check1=="c" && check2=="k"||check1=="C" && check2=="K") {
    				theWord.add(check2);
    				} else {
    					i--;
    				}
    			check1 = check2;
    		}
    				
    		for (int i = 0;i < theWord.size(); i++) {
    			word = word + theWord.get(i);
    		}
    		
    		return word;
    	}
    }
    	
    
    
    public class LetterGenerator {
    	String[] alphabet = {"a","b", "c","d", "e", "f", "g","h","i","j","k",
    			"l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
    			"","",""};
    	String[] vokal = {"a", "e", "i", "o", "u", "y", "", "", ""};
    	
    	public String generateLetter () {
    		int x = (int) (Math.random() * alphabet.length );
    		
    	return alphabet[x];
    	}
    	
    	public boolean isVokal(String s) {
    		for (int i = 0; i < vokal.length; i++) {
    			if (s == vokal[i]) {
    				return true;
    			}
    	}
    		return false;
    }
    }
    Last edited by Dowie; 05-04-2012 at 08:21 PM.

  4. #4
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Need help with a wordgenerator

    Java requires the .equals() notation, not ==.
    Strings are not considered primitives, and as such compare whether or not they are the same String, not whether they are the same value. The "a" in String[] vokal != "a" in String[] alphabet.

    Have you tested it to see if it works?

Posting Permissions

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