Results 1 to 9 of 9
Thread: need urgent help in java
- 06-22-2010, 07:36 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 2
- Rep Power
- 0
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
- 06-22-2010, 07:49 AM #2
Member
- Join Date
- May 2010
- Location
- Singapore
- Posts
- 31
- Rep Power
- 0
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
This is how i generate and print out all the 10 random letters, classmate :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;
I'm a newbie at Java too, so I can't help much ...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) ? ", " : " " )); }Last edited by poisson; 06-22-2010 at 08:01 AM.
- 06-22-2010, 08:10 AM #3
Member
- Join Date
- May 2010
- Location
- Singapore
- Posts
- 31
- Rep Power
- 0
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; }
- 06-22-2010, 12:14 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Use of switch validation is much effective in your requirement.
- 06-22-2010, 10:13 PM #5
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
- 06-23-2010, 04:04 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-23-2010, 04:05 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Give some hints and guide him first. :)
- 06-23-2010, 05:32 AM #8
- 06-23-2010, 08:00 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
[URGENT]SHA Encryption System...need urgent helps
By java_idiot in forum New To JavaReplies: 6Last Post: 05-02-2010, 10:04 AM -
Urgent Java Help Needed.
By MaN-GreaSe in forum New To JavaReplies: 8Last Post: 04-22-2010, 11:39 PM -
Sending SMS with Java-URGENT
By Robert_85 in forum Advanced JavaReplies: 1Last Post: 04-06-2009, 03:49 AM -
Urgent Java Help please
By trosity in forum New To JavaReplies: 1Last Post: 03-13-2009, 02:33 AM -
Java Beginner needs urgent help!
By Polyy in forum New To JavaReplies: 3Last Post: 11-24-2008, 03:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks