Results 1 to 4 of 4
- 06-05-2010, 09:16 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
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.
Java 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
This is a school assignment so i can't really change the main, which was written by my teacher.Java 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: "); } } }
- 06-05-2010, 10:11 PM #2
Instead of assigning the new value to the String variable, concatenate the new value to the old value:problem is that edit overwrites file
old += new; // append new to end of old
- 06-06-2010, 12:20 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
could you show me an example of how to use append effectively. Online guides aren't helping me very much.
- 06-06-2010, 02:02 AM #4
Similar Threads
-
JComboBox displaying variable two times, why?
By ecliptical in forum New To JavaReplies: 6Last Post: 01-24-2010, 06:09 PM -
calling action class multiple times
By sindhu_shiva in forum Web FrameworksReplies: 0Last Post: 08-07-2009, 02:44 PM -
Playing an AudioInputStream multiple times
By pmgallardo in forum Advanced JavaReplies: 6Last Post: 03-09-2009, 04:29 PM -
Saving JSP causing redeployment (hot deployment issue?)
By kurt_cobain in forum EclipseReplies: 1Last Post: 06-02-2008, 09:13 PM -
multiple declaration of a variable
By eva in forum New To JavaReplies: 2Last Post: 01-28-2008, 09:17 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks