Results 1 to 9 of 9
  1. #1
    formidabl3 is offline Member
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default need urgent help in java

    basically i have to do this:
    generate 10 random letters (no criteria for number of vowels) --> display the 10 letters generated --> print out how many vowels or consonents are inside.

    my code so far:

    import java.util.Scanner;
    import java.util.Random;

    public class RandomCharacters {
    char [] alphaArray = {'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'};
    char[] Vowels = {'a', 'e', 'i', 'o', 'u'};
    char[] shuffleLetter = new char[10];
    int[] randomInteger = new int[31];

    char letterArray[] = new char[ 10 ];
    String shuffledLetters = "";
    int vowelLetterCt = 0; //counting the vowel letter

    double randomNumber;
    double randomNumberSetup;
    char randomCharacter;


    Can someone help on how i should continue? thanks

  2. #2
    poisson is offline Member
    Join Date
    May 2010
    Location
    Singapore
    Posts
    31
    Rep Power
    0

    Question Not sure whether this is correct though ...

    Hey its poisson here, i'm not sure whether this is correct, it's suppose to calculate how many vowels inside

    Java Code:
    int wordLength = 0;
    int numOfVowels = 0;
    wordLength = inputWord.length();
    char [] alphaArray
    
    for (int i = 0 ; i < wordLength.length; i++)
    {
    	switch (alphaArray[i])
    			{
    				case 'A':  
    					numOfVowels = numOfVowels + 1; 					
    					break;
    				case 'E':  
    					numOfVowels = numOfVowels + 1; 					
    					break;
    				case 'I':  
    					numOfVowels = numOfVowels + 1; 					
    					break;
    				case 'O':  
    					numOfVowels = numOfVowels + 1; 					
    					break;
    				case 'U':  
    					numOfVowels = numOfVowels + 1; 					
    					break;
    				case 'B': case 'C':  case 'D':  case 'F':  case 'G':  case 'H':  case 'J':  case 'K':  case 'L':  case 'M':  case 'N':   
    					numOfVowels = numOfVowels + 0; 					
    					break;
    				case 'P':  case 'Q': case 'R': case 'S': case 'T': case 'V': case 'W': case 'X': case 'Y': case 'Z': 
    					numOfVowels = numOfVowels + 0; 					
    					break;
    			default: System.out.println("Error!!");
    					break;			
    			}/**/					
    		}	
    		return numOfVowels;
    This is how i generate and print out all the 10 random letters, classmate :

    Java Code:
    //[=====================[POOL OF 10 RANDOM LETTERS!!!]===============================================]
    			//Ensure that there are at least 2 vowels in the generated words
    			for (int k = 0; k < 2; k++){
    			index = rand.nextInt(Vowels.length);
    			shuffleLetter[k] = Vowels[index];
    		}
    			
    		//This would produce the ten random letters it's supposed to
    		//This is to initialise "i" and i is less than the length of shuffleLetter and the iterator
    		for (int i = 0; i < shuffleLetter.length; i++) 
    			{
    			int xx = rand.nextInt(alphaArray.length); 
    			//Check whether the above code works 
    			if (shuffleLetter[i] == '\u0000')
    			{
    				shuffleLetter[i] = alphaArray[xx];
    			}
    			//This would print out the letters and I added the function 
    			//Whereby the comma would be printed out for every letter
    			//until the last letter where there would not be any comma
    			System.out.print(shuffleLetter[i]+ ((i < 9) ? ", " : " " ));
    			}
    I'm a newbie at Java too, so I can't help much ...
    Last edited by poisson; 06-22-2010 at 09:01 AM.

  3. #3
    poisson is offline Member
    Join Date
    May 2010
    Location
    Singapore
    Posts
    31
    Rep Power
    0

    Exclamation Maybe this is how you count vowels in a word or string?

    Java Code:
    int numOfVowels(String text) {
        int num = 0; // start the number at zero
      
        for (int i = 0; i < wordInput.length(); i++) {
            char c = wordInput.charAt(i);
            if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
               num++;
            }
        }
        return num;
    }
    :)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    This will output the 10 random letters and tell you how many vowels and constants.

    i'll explain how it works later, g2g camping now

    PHP Code:
    package getdata;
    
    import java.util.Random;
    
    public class Main {
    
    
        public static void main(String[] args) {
    
            final int MAX = 10;
            
            char[] mixedAlpha = new char[MAX];
    
            mixedAlpha = getRandom(mixedAlpha);
    
    
            int cont=0;
            int vow =0;
    
            for(int i=0;i < mixedAlpha.length;i++){
    
                switch(mixedAlpha[i]){
                    case 'a':
                        vow+=1;
                        break;
                    case 'e':
                        vow+=1;
                        break;
                    case 'i':
                        vow+=1;
                        break;
                    case 'o':
                        vow+=1;
                        break;
                    case 'u':
                        vow+=1;
                        break;
                    default:
                        cont+=1;
                }
                System.out.println(mixedAlpha[i]);
            }
    
                System.out.println(vow + "  " + cont);
    
        }
        private static char[] getRandom(char[] charArray){
    
            char[] alphabet= "abcdefghijklmnopqrstuvwxyz".toCharArray();
            Random r = new Random();
            char a = 'a';
    
            for(int i=0;i < charArray.length;i++){
                charArray[i] =(char) (r.nextInt(alphabet.length) + a );
            }
            return charArray;
        }
    
    }
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    alacn, I think it's better to leave for OP to tryout something first of all. I'm really appreciate your help here but in this way OP may not learn something new.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  8. #8
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by alacn View Post
    This will output the 10 random letters and tell you how many vowels and constants.

    i'll explain how it works later, g2g camping now

    PHP Code:
    package getdata;  //name of package
    
    import java.util.Random; //importing the random class
    
    public class Main {
    
    
        public static void main(String[] args) {
    
            final int MAX = 10; //set constant at 10
            
            char[] mixedAlpha = new char[MAX]; //array to store the 10 random 
                                                                 //letters
            
    //putting the empty array into a method i made called getRandom and the //method returns 10 random letters back into mixedAlpha array
           mixedAlpha = getRandom(mixedAlpha);  
                                                  
    //a couple of variables to store the amount of vowels and constants //generated in code below
            int cont=0;
            int vow =0;
    
    //loops through the mixedAlpha array and adds +1 to the variable vow //(vowels) everytime it comes across a vowel, if it doesnt come across any
    //vowels then the code adds +1 to the cont variable (constants)
            for(int i=0;i < mixedAlpha.length;i++){
    
                switch(mixedAlpha[i]){
                    case 'a':
                        vow+=1;
                        break;           //must break after each one
                    case 'e':
                        vow+=1;
                        break;
                    case 'i':
                        vow+=1;
                        break;
                    case 'o':
                        vow+=1;
                        break;
                    case 'u':
                        vow+=1;
                        break;
                    default:
                        cont+=1;
                }
                System.out.println(mixedAlpha[i]); //after every interation (loop)
                                                         //current letter is outputed to console
            }
    
    //after all loops completed, the amount of vowels and constants are outputed
                System.out.println(vow + "  " + cont);
    
        }
    //the actual method which does all the main work
    //we passed in the array mixedAlpha into it and this method sees that array 
    //as charArray for the perpose of the below code
        private static char[] getRandom(char[] charArray){
    
    //created a char array called alphabet and used a built in method called 
    //toCharArray(); which basically splits the string into individual chars
    //so that alphabet[0] = 'a', alphabet[1] ='b' and so on
            char[] alphabet= "abcdefghijklmnopqrstuvwxyz".toCharArray();
    
    //create an object called r from the Random class which we imported at the //top
            Random r = new Random();
    
    //created a character type called a and stored the value a in it so that 
    //i can use it to add with in code later
            char a = 'a';
    
    //loops 10 times
            for(int i=0;i < charArray.length;i++){
    //for each loop r.nextInt generated a random number between 0 and 25
    //because thats 26 values (we get the 26 from alphabet.length
    //we then add the char type a to it because when you add a CHAR and an //INT you get a CHAR. for example if r.nextInt were to generate the number 2
    //then you would get (2 + 'a') which would give you 'c' then the (char) thingy
    //ensures the result is read/converted into a char type so that in can be //stored in charArray. This is repeated 10 times until array is full
                charArray[i] =(char) (r.nextInt(alphabet.length) + a );
            }
    //once the loop is over the entire array is returned out of the loop back //into mixedArray     
            return charArray;
        }
    
    }
    hope the above code helps with your learning.
    thankyou for the tip Eranga, i will do in future
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Spoiler code ahead so no comments or excplanations:

    Java Code:
    String s= ... /* only letters in here */
    int vowels= s.replaceAll("[^aeiou]", "").length();
    int consonants= s.length()-vowels;
    kind regards,

    Jos

Similar Threads

  1. [URGENT]SHA Encryption System...need urgent helps
    By java_idiot in forum New To Java
    Replies: 6
    Last Post: 05-02-2010, 11:04 AM
  2. Urgent Java Help Needed.
    By MaN-GreaSe in forum New To Java
    Replies: 8
    Last Post: 04-23-2010, 12:39 AM
  3. Sending SMS with Java-URGENT
    By Robert_85 in forum Advanced Java
    Replies: 1
    Last Post: 04-06-2009, 04:49 AM
  4. Urgent Java Help please
    By trosity in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 03:33 AM
  5. Java Beginner needs urgent help!
    By Polyy in forum New To Java
    Replies: 3
    Last Post: 11-24-2008, 04:11 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
  •