Showing posts with label String[]. Show all posts
Showing posts with label String[]. Show all posts

Tuesday, October 6, 2009

Java - pig latin generator

 
package javasomethingsomething;
 
public class Main {
    public static void main(String[] args) {
        // all the heavy lifting is done in the PigLatin class 
        PigLatin pg = new PigLatin();
        String pigSentence = "";
        String sentence = "This is going to be a pig latin sentence";
        // iterate over the sentence with for-each and split by " " 
        for (String s:sentence.split(" ")) {
            pigSentence += pg.getPigLatinForWord(s) + " ";
        }
        // added the trim to remove the trailing space 
        System.out.println(pigSentence.trim());
    }
 
}
 
class PigLatin{
 
    public String getPigLatinForWord(String word) {
        int firstVowel = word.length();
        // yes, I included y as a vowel!! 
        String[] vowels = {"a", "e", "i", "o", "u", "y"};
        for(String s:vowels){
            // if indexOf() finds no match it returns -1 
            if ((word.indexOf(s) < firstVowel) && (word.indexOf(s) != -1)) {
                firstVowel = word.indexOf(s);
            }
        }
        return  word.substring(firstVowel, word.length()) +
                word.substring(0, firstVowel) +
                "ay";
    }
}
 

Friday, September 25, 2009

Java - reverse the word order of a sentence

public class Main {
 
    public static void main(String[] args) {
        Words w = new Words();
        String sentence = "The example sentence is here";
        System.out.println(sentence);
        System.out.println("reversed to:");
        System.out.println(w.reverseWordOrderOfSentence(sentence));
    }
}
 
class Words{
    public String reverseWordOrderOfSentence(String sentence){
        // split up by words 
        String[] words = sentence.split(" ");
        sentence = "";
 
        // iterate backward through the array 
        // rebuilding the sentence from the back forward 
        for (int i=words.length; i>0; i--){
            sentence += words[i-1];
            sentence += " ";
        }
        // remove the trailing white space 
        return sentence.trim();
    }
}
 
/* 
 *      OUTPUT: 
        The example sentence is here 
        reversed to: 
        here is sentence example The 
*/ 
 

Java - remove vowels from a sentence String

public class Main {

    public static void main(String[] args) {
      NotStatic g = new NotStatic();
      String sentence = "This is my example sentence.";
      String s = g.RemoveVowelsFromSentence(sentence);
      System.out.println(s);

      // output:

      //    Ths s m xmpl sntnc.

    }
}
class NotStatic {

    public String RemoveVowelsFromSentence(String sentence) {
        String[] vowels = {"a", "e", "i", "o", "u", "y"};
        // iterate through the vowel array with for-each

        for (String s:vowels){
            sentence = sentence.replaceAll(s, "");
        }

        return sentence;

    }

}


Thursday, September 24, 2009

Java - create and use a String or int array

 
public class Main {
    public static void main(String[] args) {
        // standard, fast, old school array 
        // string array 
        String[] stringArray = new String[10];
        stringArray[0] = "first item in array";
        System.out.println("array length: " + stringArray.length);
        // indicates:  10 
        // even if you've only populated 
        // one of the you allocated room for 10 
 
        // you can also for loop through and populate 
        String tempString = "";
        for (int i=0; i<stringArray.length; i++){
            tempString = Integer.toString(i+1);
            stringArray[i] = "arrayitem " + tempString;
        }
 
        // display contents of array 
        //  with a for each 
        for (String s:stringArray){
            System.out.println(s);
        }
 
 
        // same methodology for an int array 
        int[] intArray = new int[10];
        intArray[0] = 213;
    }
 
}