• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

String Minipulation

Hey Everyone!

I found this tutorial the other day dealing with converting english to piglatin, but I can't find it for the life of me now.  I'm really interested in doing it and I thought I'd stop by here to get some advice on what actually needs to be done.

I'm just trying to get an idea on what needs to be done and with what.  For example, here is something that has to be looked for in the user input.

---------------------------
For words that begin with a vowel (a,e,i,o,or u) and end with a vowel. First, add a y at the beginning of the word. Then add yay at the end.

For example:  User inputs a word like, 'orange'.   Output would be 'yorangeyay'
---------------------------

I'm just trying to get an idea on what I need to use to get it going or what it would look like and possibly what you think about when you see a problem like this.

Regards,
David McGraw
0
ildave1
Asked:
ildave1
  • 5
  • 3
2 Solutions
 
ashok3sepCommented:
public class VowelsTest {
      public static char[] vowels = {'a','e','i','o','u'};
      
      public static void main(String[] args)
      {
            String input = args[0];
            char[] inputs = input.toCharArray();
            for(int i = 0; i < vowels.length; i++)
            {
                  if(inputs[0] == vowels[i])
                        input = "y"+input;
                  if(inputs[inputs.length - 1] == vowels[i])
                  {
                        input = input.substring(0,input.length()-1)+"y"+inputs[inputs.length - 1]+"y";
                  }
            }
            System.out.println("input =" +input);      
      }
      
}
0
 
ildave1Author Commented:
Hi Ashok3sep.

I'm not so much interested into seing the whole code, but rather more interested into understanding how you read a word like, 'orange' , and then seeing that it begins and ends with a vowel, and then putting y on the front of the word and yay on the end of the word.

Thank you,
David McGraw
0
 
ashok3sepCommented:
Here you get the User Input in to a string
>>>>args[0];

convert to a CharArray();

then take the first character of that array and then check whether the letter is a vowel or not

if it is a vowel then you add a letter y to the from of that array.

then move to a last character of that array and again check for the vowel or not and repeat the same


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
WebstormCommented:
Hi ildave1,

What you need is calling methods of the String class :
    length()            to get the length of the String
    charAt( index )  to get the character at the specified index ( from 0 to length()-1 )
    + is used to concat two or many String values

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
0
 
ildave1Author Commented:
Alright! Im back from vacation and I've trying to work on this tutorial.  So far I've set up the foundation, but geez, as far as understanding how to implement this is proving to be a difficult task.  Here is the small portion of this that I've worked so far.  Im trying to set it up in MCV architecture to be able to add it to my reference library of tutorials.  Here is what Ive gotten so far;

Any different ways to explain this would help greatly.

Thanks,
Dave

=========================================
Control:
public class Control {
      public static void main(String[] args) {
            
      // Declare Variables
            View view;
            Model stringmod;
            String input;
            boolean done = false;
            
      // Creating Objects
            view = new View();
            stringmod = new Model();
            
    // Get Input String from User(View Class)
            input = view.getInputString();
            stringmod.setInput(input);
            
            
            
      }
}
========================================
View:
import java.io.*;


public class View {
      private BufferedReader reader;
      
      // Creating BufferedReader for Input
      public View() {
            reader = new BufferedReader(new InputStreamReader(System.in));
      }
      
      // Getting Input from User
      public String getInputString() {
            String str = "";
            
            System.out.print("Please Input a String to be Translated: ");
            try {
                  str = reader.readLine();
            }
            catch(IOException ioe) {
                  System.out.println("Input Error!!");
            }
            
            return str;
      }
      
      // Displays Modified Word
      public void displayMod(String str){
            
            
      }
      
}
====================================
Model:
public class Model {
      private String str[];
      
      /**
       * Setting Input
       *
       * @param newString - the new value for str
       */
      public void setInput(String[] newString){
             str = newString;
      }
      
      public void checkVowel(String Vowel){
            char[] vowels = {'a','e','i','o','u'};
            
            
            
            
            
      }
      
}
0
 
ashok3sepCommented:
In the VIEW class:-
>>>>Instead of this line------> str = reader.readLine();

try out this one..........
while ((str = input.readLine()) != null)
{
// Do proecessing of the Input String from the User......
}

In the CheckVowel Mehtod implement the code for checking the String you get from the User............

0
 
ildave1Author Commented:
Well the reason I did it that was mainly for the MCV understanding.  Wouldn't I use the Model method to do the while statement (the central part of the program)?
0
 
ildave1Author Commented:
Maybe this will be easier for me to understand.  Here the actual tutorial, but its using a GUI that I really dont want to get into yet because im trying to get the basics down first.  If it would be easier, could you edit it, or would that not be easy?  I'm not sure what needs removed/replaced.

Regards,
Dave

----
import java.awt.*;        // Container, FlowLayout
import java.awt.event.*;  // ActionEvent, ActionListener
import javax.swing.*;     // JApplet, JButton, JLabel, JTextField
import java.util.*;       // StringTokenizer, Pattern



public class PigLatin extends JFrame {


   JLabel promptLabel;
   JTextField inputField;
   JTextArea outputArea;
   String latinWord="";
   String translatedSentence="";

   // calling the superclass' constructor to setup and instantiate GUI

   public PigLatin()
   
   {

      // call JFrame constructor to set title bar string
      super( "Kelli Wiseth Pig Latin Generator" );
            

      // obtain content pane and set layout to FlowLayout

      Container container = getContentPane();
      container.setLayout( new FlowLayout(FlowLayout.LEFT, 10, 12) );

       
      promptLabel = new JLabel( "Enter the word or phrase you want translated to PigLatin:");


      container.add( promptLabel );
        inputField = new JTextField( 33 );
      inputField.addActionListener(

      new ActionListener() {      // anonymous inner class for event handling

      // event handler for text field
      public void actionPerformed( ActionEvent event )
      {
      StringTokenizer pre_latin_tokens = new StringTokenizer( event.getActionCommand(), " \t\n\r\f!?.," );
      outputArea.setText("");
           
      while (pre_latin_tokens.hasMoreTokens())
            {
            latinWord = printLatinWord(pre_latin_tokens.nextToken());
            translatedSentence += latinWord;
            }// closing brace for while
           
            // display the complete, translated phrase
            outputArea.setText(translatedSentence = translatedSentence + "\n");
         
             
      } // end method actionPerformed

    } // end anonymous inner class

  ); // end call to addActionListener

 
  container.add(inputField);
  outputArea = new JTextArea( 15, 33 );
  outputArea.setEditable( false );
 
 
  container.add(new JScrollPane(outputArea));
 
  setSize( 400, 370 );  // set the window size
  setVisible( true );   // show the window


 }// end constructor PigLatin


      public String printLatinWord(String word_input)
      {
     String latinWord = "";
     String first_letter = "";
     String pre_pig_latin_phrase = word_input;

     first_letter = word_input.substring(0,1);
   
      if ((first_letter.matches("[aeiou]")) || (first_letter.matches("[AEIOU]")))
        {
        latinWord = word_input + "way ";
        }
      else
        {
            latinWord = word_input.substring(1) + word_input.substring(0,1) +  "ay ";
        }

    return(latinWord);

      } // closing brace printLatinWord method


      public static void main(String args[]) {
            PigLatin application = new PigLatin();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


      } // closing brace main method


} // closing brace class PigLatin
0
 
ildave1Author Commented:
Here is what Ive came up with, but it doesnt seem to return the modification.  Any idea?

--------------------------------------------
Control
----------------------------------------------
public class Control {
      public static void main(String[] args) {
            
      // Declare Variables
            View view;
            Model model;
            String str;
            boolean done = false;
            
      // Creating Objects
            view = new View();
            model = new Model();
            
    // Get Input String from User(View Class) and Give it to Model to Check
            str = view.getInputString();
            model.checkStr(str);
            
    // Give Result to View to Display            
            view.displayMod(str);
            
            
      }
}

--------------------------------------------
View
--------------------------------------------
import java.io.*;

public class View {
      private BufferedReader reader;
      
      // Creating BufferedReader for Input
      public View() {
            reader = new BufferedReader(new InputStreamReader(System.in));
      }
      
      // Getting Input from User
      public String getInputString() {
            String str = "";
            
            System.out.print("Please Input a String to be Translated: ");
            try {
                  str = reader.readLine();
            }
            catch(IOException ioe) {
                  System.out.println("Input Error!!");
            }
            
            return str;
      }
      
      // Displays Modified Word
      public void displayMod(String str){
            System.out.println(str);
            
      }
      
}

--------------------------------------------
Model
-------------------------------------------
import java.util.*;

public class Model {
      private String str;
      
      public String checkStr(String str){
             String latinWord = "";
           String first_letter = "";
           String pre_pig_latin_phrase = str;

           first_letter = str.substring(0,1);
          
             if ((first_letter.matches("[aeiou]")) ||  (first_letter.matches("[AEIOU]")))
              {
              latinWord = str + "way ";
              }
            else
              {
            latinWord = str.substring(1) + str.substring(0,1) +  "ay ";
              }

          return(str);

            }      
            
      }
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now