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

    Default copying a part of a string and moving it to the end of the string?

    If I have a String name "sentence" which contained the words: "Translate the string to Pig Latin"

    How would I select the consonants or every letter before a vowel and move it behind the word, like this: (the apostrophe and caps are added to shows the letters i moved to the back of the word)


    sentence = anslate'TR' e'TH' int'ST' o'T' ig'P' atin'L'

    I was thinking about putting it into an array of words

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    How about creating a method
    Java Code:
    public static boolean isVowel(char c) {
       // code goes here to see if c is a vowel.
    }
    and then loop through each word's characters until a vowel is reached.

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

    Default

    I already have a 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: ");
    		}
    	}
    }
    And this is my code:

    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 "Sid Patel";
    	}
    	
    	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++){
    				
    			sentence = Character.toUpperCase(sentence.charAt(i)) + sentence.substring(1);
    		
    			}
    			return sentence;
    			
    			}
    			
    		else 
    			return sentence;
    		}
    		
    	}
    I want to get
    Java Code:
    else if (translator == 3){
    			
    			
    			for (int i = 0; i < sentence.length(); i++){
    				
    			sentence = Character.toUpperCase(sentence.charAt(i)) + sentence.substring(1);
    		
    			}
    			return sentence;
    working so that it will return the characters in an alternating case. for example "CaSe". I got the first letter working, but don't know how to loop it so that all the letters will work

  4. #4
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Quote Originally Posted by sidd0123 View Post
    working so that it will return the characters in an alternating case. for example "CaSe". I got the first letter working, but don't know how to loop it so that all the letters will work
    Java Code:
            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 ());
                }
            }
            return sentence;
    if(1 % 2 == 2) simply finds if the remainder of i / 2 = 0, in other words, finds if it is an even character, and this line that swaps the character will be skipped for odd values of i.

    By the way, have you just given up on the igpay atinlay ?

    Cheers.
    Last edited by Cruncher; 04-19-2010 at 08:54 AM.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by Cruncher View Post
    Java Code:
            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 ());
                }
            }
            return sentence;
    Do this right and use a StringBuilder, though. You may as well learn good habits when you're starting, rather than having to unlearn bad habits later.
    Java Code:
                StringBuilder sb = new StringBuilder(sentence.length());
                int i = 0;
                for (char c : sentence.toCharArray()) {
                	if (i++ % 2 == 0) {
                		sb.append(Character.toUpperCase(c));
                	} else {
                		sb.append(Character.toLowerCase(c));
                	}
                }
                sentence = sb.toString();
    -Gary-

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

    Default

    Thanks, it definitely works great and nope, I haven't forgotten that Pig Latin translator part, just haven't gotten to it yet.

    I do have a quick question though. What makes:

    Java Code:
    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 ());
                }
            }
            return sentence;
    Originally posted by Cruncher

    and this one here better if they accomplish the same thing in different ways:

    Java Code:
    StringBuilder sb = new StringBuilder(sentence.length());
                int i = 0;
                for (char c : sentence.toCharArray()) {
                	if (i++ % 2 == 0) {
                		sb.append(Character.toUpperCase(c));
                	} else {
                		sb.append(Character.toLowerCase(c));
                	}
                }
                sentence = sb.toString();
    Originally posted by gcalvin
    Last edited by sidd0123; 04-20-2010 at 04:03 AM.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    It's a little complicated to explain the differences in efficiency. The first approach seems to be updating the String "in place" but remember that Java Strings are immutable, which means that substring() has to create a new String at each invocation and you're building a new String sentence each time through the loop. Also, appending Strings with + is considerably less efficient than StringBuilder's append() method. Not that efficiency should be your first concern when you're learning, but it's just a good habit when you're doing a lot of appending on a String to use a StringBuilder instead.

    (Also, the first version leaves the character unchanged if it's an odd position, while the second version makes sure it's lower case.)

    -Gary-

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

    Default

    I used the following code and it fixes the issue :
    (Also, the first version leaves the character unchanged if it's an odd position, while the second version makes sure it's lower case.)
    Java Code:
    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;
    :)

    For the pig latin translator, could I possibly use another substring and remove the consonants before the vowel using a method from the characters class file form Java docs?

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

    Default

    Anyone here???

  10. #10
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Okay i'm not going to code this i'll explain this is pseudo, if you have problems with it then we'll talk code.

    basically, you need to loop through each character of the word until you hit vowel. so you will need a method that should probably return a boolean, and pass it a character, this method should return true if it's a vowel and false if it's not. so just break out of the loop when the ith character is a vowel and then move everything before it to the back using substring.

    If you need help with that, post back.

Similar Threads

  1. unicode as part of string
    By mac in forum New To Java
    Replies: 0
    Last Post: 01-28-2010, 02:23 AM
  2. search with part of string
    By virendra in forum Lucene
    Replies: 1
    Last Post: 01-21-2010, 12:56 PM
  3. Replies: 1
    Last Post: 11-26-2009, 05:05 PM
  4. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  5. How to get part of a String?
    By eva in forum New To Java
    Replies: 1
    Last Post: 12-23-2007, 06:58 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
  •