Issue with saving multiple times to a variable
I have a variable called sentence. In sentence I have 2 words say (file / edit).
I have code that uses a pig latin translation on those two words, so file and edit now look like this
ilefay
editay
google pig latin to see how it works. basically take all consonants and move to back and add ay to the end. if the first letter is a vowel, then just add ay.
then i return sentence to my main. My code changes one word at a time by changing one word into an array of characters and cross checking with the vowels.
Once the first word, File is done it is saved into string variable sentence. then it works on edit. and saves into sentence.
The problem is that edit overwrites file. How do i get it so that file and edit show up in a single line.
Code:
package package10;
public class Xlator {
public static final int UNCHANGED = 0;
public static final int LOWERCASE = 1;
public static final int UPPERCASE = 2;
public static final int ALTERNATE = 3;
public static final int PIGLATIN = 4;
public static String getAuthor(){
return "*****";
}
public static String Xlate(int translator, String sentence){
if (translator == 1){
return sentence.toLowerCase();
}
else if (translator == 2){
return sentence.toUpperCase();
}
else if (translator == 3){
for (int i = 0 ; i < sentence.length () ; i++)
{
if (i % 2 == 0)
{
sentence = sentence.substring (0, i) + Character.toUpperCase (sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ());
}
if (i % 2 != 0)
{
sentence = sentence.substring (0, i) + Character.toLowerCase(sentence.charAt (i)) + sentence.substring (i + 1, sentence.length ());
}
}
return sentence;
}
else if (translator == 4){
String [] words = sentence.split("[ .,?!]+");
String statement;
char [] vowels = {'a','e','i','o','u'};
char temp;
for(int door = 0; door <= (words.length -1); door = door + 1){
char[] letters = words[door].toCharArray();
if (letters [0] == vowels [0]){
sentence = new String (letters) + "ay";
}
else if (letters [0] == vowels [1]){
sentence = new String (letters) + "ay";
}
else if (letters [0] == vowels [2]){
sentence = new String (letters) + "ay";
}
else if (letters [0] == vowels [3]){
sentence = new String (letters) + "ay";
}
else if (letters [0] == vowels [4]){
sentence = new String (letters) + "ay";
}
do{
temp = letters [0];
for (int a = 0; a <= (letters.length - 2); a = a + 1){
letters [a] = letters [a + 1];
}
letters[letters.length - 1] = temp;
sentence = new String (letters) + "ay";
}
while (letters [0] != vowels [0] && (letters [0] != vowels [1]) && (letters [0] != vowels [2]) && (letters [0] != vowels [3]) && (letters [0] != vowels [4]));
}
}
return sentence;
}
}
THIS IS MY MAIN
Code:
package package10;
import javax.swing.JOptionPane;
public class XlatorMain {
/**
* @param args
*/
public static void main(String[] args) {
// if you don't have one (or more) of these translators implemented, jut comment out the line
int [] translator = {
Xlator.UNCHANGED,
Xlator.LOWERCASE,
Xlator.UPPERCASE,
Xlator.ALTERNATE,
Xlator.PIGLATIN,
//Xlator.SWEDISHCHEF
};
String input = JOptionPane.showInputDialog(null,"Enter the text for translation: ");
while (input!= null) {
String outputString = "";
for (int j = 0; j < translator.length; j++) {
String ans1 = Xlator.Xlate(translator[j], input);
outputString = outputString + "\n'" + input + "' translates to '" + ans1 + "' using method " + translator[j];
}
input = JOptionPane.showInputDialog(null,outputString + "\n\nEnter the text for translation: ");
}
}
}
This is a school assignment so i can't really change the main, which was written by my teacher.