Results 1 to 4 of 4
  1. #1
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default 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


    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: ");
    		}
    	}
    }
    This is a school assignment so i can't really change the main, which was written by my teacher.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default

    problem is that edit overwrites file
    Instead of assigning the new value to the String variable, concatenate the new value to the old value:
    old += new; // append new to end of old

  3. #3
    sidd0123 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    could you show me an example of how to use append effectively. Online guides aren't helping me very much.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default

    Given:
    String old = "asdf";
    String new = "def";
    old += new; // append new to end of old
    old now is "asdfdef";

Similar Threads

  1. JComboBox displaying variable two times, why?
    By ecliptical in forum New To Java
    Replies: 6
    Last Post: 01-24-2010, 06:09 PM
  2. calling action class multiple times
    By sindhu_shiva in forum Web Frameworks
    Replies: 0
    Last Post: 08-07-2009, 02:44 PM
  3. Playing an AudioInputStream multiple times
    By pmgallardo in forum Advanced Java
    Replies: 6
    Last Post: 03-09-2009, 04:29 PM
  4. Replies: 1
    Last Post: 06-02-2008, 09:13 PM
  5. multiple declaration of a variable
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 09:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •