Solved

# String Minipulation

Posted on 2005-03-05
Medium Priority
241 Views
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
Question by:ildave1
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 5
• 3

LVL 5

Accepted Solution

ashok3sep earned 1400 total points
ID: 13469484
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

LVL 3

Author Comment

ID: 13471646
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

LVL 5

Expert Comment

ID: 13471901
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

LVL 13

Assisted Solution

Webstorm earned 600 total points
ID: 13476840
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

LVL 3

Author Comment

ID: 13527358
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 {

// Creating BufferedReader for Input
public View() {
}

// Getting Input from User
public String getInputString() {
String str = "";

System.out.print("Please Input a String to be Translated: ");
try {
}
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

LVL 5

Expert Comment

ID: 13527846
In the VIEW class:-

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

LVL 3

Author Comment

ID: 13529834
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

LVL 3

Author Comment

ID: 13531849
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:");

inputField = new JTextField( 33 );

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

outputArea = new JTextArea( 15, 33 );
outputArea.setEditable( false );

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

LVL 3

Author Comment

ID: 13531942
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 {

// Creating BufferedReader for Input
public View() {
}

// Getting Input from User
public String getInputString() {
String str = "";

System.out.print("Please Input a String to be Translated: ");
try {
}
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
###### Suggested Courses
Course of the Month10 days, 10 hours left to enroll

#### 765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.