Results 1 to 6 of 6
  1. #1
    sselasky1 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Can't figure out how to change a character in string from upper to lower & vice versa

    Hello, my name is Shane and I can't figure out how to take a string and convert each character from uppercase to lowercase and vice versa. For example AbCdE would convert to aBcDe. I am using JTextFields to do this. I didn't paste the GUI code since I am not having an issue with that but if you need me to paste the whole program I will. Any suggestions will be appreciated. Thanks.
    XML Code:
    
     public void actionPerformed(ActionEvent e)
          {
        	 //declare variable
    
        	 int length, i;
        	 String str;
                 char ch;
            
                 //get input from text field
             str = string1TF.getText();
           
            //loop through string by each character
             for (i = 0; i < str.length(); i++)
             {
                   
                  //get character at position i
            	 ch = str.charAt(i);
            	
                  // determine if letter is uppercase or lowercase and replace with opposite letter
            	 if (ch == Character.toUpperCase(ch))
            		 str.replace(ch, Character.toLowerCase(ch));
            	 else 
                	//	 str.replace(ch, Character.toUpperCase(ch));
            		 
             }
             
                 //show string in text field
            	string2TF.setText(str);
    
            }

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

    Default Re: Can't figure out how to change a character in string from upper to lower & vice v

    Burn this into your brain: Strings are immutable -- meaning they can't be changed. Ever.

    You're calling String#replace(...) method, but from the rule I mention above, you now know that this doesn't change the String that has the method called on it. No instead it returns a String with the changes made. If you want the original String variable to refer to this new String you have to assign it to do this. e.g.,

    Java Code:
    myString = myString.replace("a", "A");

  3. #3
    sselasky1 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Can't figure out how to change a character in string from upper to lower & vice v

    Quote Originally Posted by Fubarable View Post
    Burn this into your brain: Strings are immutable -- meaning they can't be changed. Ever.

    You're calling String#replace(...) method, but from the rule I mention above, you now know that this doesn't change the String that has the method called on it. No instead it returns a String with the changes made. If you want the original String variable to refer to this new String you have to assign it to do this. e.g.,

    Java Code:
    myString = myString.replace("a", "A");
    Shane: Burned into my head. But what about if the string is AaA. How would I go about changing the string to aAa?
    Last edited by sselasky1; 02-16-2012 at 12:59 AM.

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

    Default Re: Can't figure out how to change a character in string from upper to lower & vice v

    Quote Originally Posted by sselasky1 View Post
    Shane: Burned into my head. But what about if the string is AaA. How would I go about changing the string to aAa?
    My code was just an example. Please don't take it as a literal example. Use your powerful brain and the ideas we've given you and modify your code. I'll bet that you'll be able to figure it out. Please let us know though either way what happens.

  5. #5
    sselasky1 is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Can't figure out how to change a character in string from upper to lower & vice v

    Quote Originally Posted by Fubarable View Post
    My code was just an example. Please don't take it as a literal example. Use your powerful brain and the ideas we've given you and modify your code. I'll bet that you'll be able to figure it out. Please let us know though either way what happens.
    shane: Got it. However, when I cycle loop through my string, all the letters in the string are converting all to uppercase or lowercase. Is this a result of what you said earlier?

    XML Code:
              // loop through string
             for (i = 0; i < str.length(); i++)
             {
                  //get char
            	 ch = str.charAt(i);
            	 
            	// check char if upper or lowercase and convert to opposite letter
            	 if (ch == Character.toUpperCase(ch))
            		str = str.replace(str.charAt(i), Character.toLowerCase(str.charAt(i)));
            	 else 
                	 str = str.replace(str.charAt(i), Character.toUpperCase(str.charAt(i)));
            		 
             }

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

    Default Re: Can't figure out how to change a character in string from upper to lower & vice v

    You need to look at the String API to see just what replace does as it's doing a lot more than you think it's doing. Myself, I'd use a StringBuffer for this problem, not a String since it is mutable, and since it can be easily changed into a String via its toString() method.

Similar Threads

  1. Making string insensitive to upper or lower case
    By ahmedaa16 in forum New To Java
    Replies: 0
    Last Post: 10-21-2011, 12:59 PM
  2. Replies: 5
    Last Post: 04-16-2009, 06:30 PM
  3. Postfix into prefix and vice versa
    By sfe23 in forum New To Java
    Replies: 9
    Last Post: 02-19-2009, 10:37 PM
  4. Converting to ASCII and vice-versa
    By pheonix in forum New To Java
    Replies: 2
    Last Post: 09-09-2008, 04:43 AM
  5. Document conversion PDF to MS doc and vice versa
    By abintoms in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:45 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
  •