Join 149,606 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,907 people online right now. Registration is fast and FREE... Join Now!
I may have some syntax incorrect (if so Im sorry) but couldnt you create an array populated wiht the vowels, then loop through the array checking each index to your myString.charAt(0), something like this
CODE
char vowels[] = {'a','e','i','o','u'}; char chr = myString.charAt(0); for(int i = 0;i < vowels.length;i++) { if vowels[i] == chr { System.out.println("the first character is a vowel"); } }
just trying to see if the first character is a vowel or not, don't remember java after the break and its kicking my ass....
so i was just doing an if statement, seeing if the char was equal, but i get the error that the || operator can't be used between java.lang.strings
CODE
char firstChar = userText.charAt(0); if(firstChar = "a" || "A" || "e" || "E" || "i" || "I" || "o" || "O" || "u" || "U" ){ System.out.println("the first character is a vowel");}
Three problems: - "a" is a String not a char. 'a' is the char. - the equality operator is == and not = - you have to write the whole thing like this: (firstChar==a || firstChar == A || ... )
Naturally it would be easier to test it with either with a switch (probably it would be faster) or writing something like this:
I would recommend having a look at the Character and String class, they have very useful functions that generally do right what you are looking for (telling you if a symbol is a letter or digit, and converting between upper and lower case)
this is for myself. restricted comp access right now.
note to self : ignore last section & Fix find first vowel
CODE
import java.util.*;
public class PigLatin{ public static void main(String[] args){ //Establish a scanner & prompt for input Scanner scan = new Scanner(System.in); System.out.println("Please Enter A Sentence:"); String toTranslate = scan.nextLine();
//Establish an object and print out return from running it PigLatin translateIt = new PigLatin(); System.out.println(translateIt.translate(toTranslate)); }
private String translatedString = ""; //Will become the final string private String userText; //the input from the user private String finalWord; //Will become each word after translated private int startWord = 0; private int endWord = 0; private int firstVowel = 0; //marks the first vowel in each word private char vowelCheckChar =0; //used as a marker of what letters have been checked to be vowels
// Translate an individual word public String translateWord(String userWord){
//create an object and then pass word to findFirstVowel PigLatin wordIt = new PigLatin(); firstVowel = wordIt.findFirstVowel(userWord);
//If the first letter in the word is a vowel, follow piglatin rules //and create the final word if(firstVowel == 0 ){ finalWord = (userWord + "yay"); }
//If the first letter is a consonant, split the word at the firstVowel, //and build a finalWord else{ String beforeVowel = userWord.substring(0,firstVowel); String afterVowel = userWord.substring(firstVowel); finalWord = (afterVowel + beforeVowel + "ay"); }
return finalWord; }
//translate a whole string of words private String translate(String userWord){ userWord = userWord;
//this will repeat as long as the endWord (which marks the last letter of the last word translated) //is less than the total number of letters in the string for(int i = 0; endWord < userWord.length(); i++){
//re-establish the end of our new word as the next space endWord = (userWord.indexOf(" ", startWord));
//if the indexOf returns a false statement, then there is no final space //so we extend the endWord only to the last character in the string if(endWord == -1){ endWord = userWord.length(); }
//now we know both ends of where our word is, we pull the string out of there String nextWord = userWord.substring(startWord,endWord); //for the next loop we establish the new startpoint startWord = (endWord + 1); //create a piglatin object PigLatin wordIt = new PigLatin(); //use the translateWord method to translate each individual word, //pass it the string we just cut out of the user entry translatedString += wordIt.translateWord(nextWord); //since we culled out the spaces, put one back in translatedString += " "; } return translatedString; }
private int findFirstVowel(String userWord) { //Establish array of vowels char vowels[] = {'a','e','i','o','u'};
//Runs through each letter of the word until a vowel is found for(int p=0; p < userWord.length(); p++){ vowelCheckChar = userWord.charAt(p);
//Compares the letter currently on, to the array of vowels //if a match is found, the loop will end and the firstVowel marked for(int q=0; q < vowels.length; q++){ if (vowels[q] == vowelCheckChar) { firstVowel = p; p=userWord.length(); } } } return firstVowel; } }
This post has been edited by Tyler James Lee: 17 Sep, 2007 - 09:52 AM
so i have all this stuff going on in my PigLatin class, as is specified for the project,
but the class also is supposed to have a main method to run the little program (normally we put the main in a little test file and our class somewhere else)
anyways, you can see before i use one method inside another, i initialize a new object, and then do it like that.
is that necessary? if not is there another way to use one method within another cause i didn't get it going.
CODE
PigLatin wordIt = new PigLatin(); firstVowel = wordIt.findFirstVowel(userWord);