Results 1 to 6 of 6
- 02-16-2012, 12:43 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
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); }
-
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");
- 02-16-2012, 12:57 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
-
Re: Can't figure out how to change a character in string from upper to lower & vice v
- 02-16-2012, 01:27 AM #5
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Re: Can't figure out how to change a character in string from upper to lower & vice v
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))); }
-
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
-
Making string insensitive to upper or lower case
By ahmedaa16 in forum New To JavaReplies: 0Last Post: 10-21-2011, 12:59 PM -
Retrieving all the keys corresponding to a value and vice versa
By Ms.Ranjan in forum New To JavaReplies: 5Last Post: 04-16-2009, 06:30 PM -
Postfix into prefix and vice versa
By sfe23 in forum New To JavaReplies: 9Last Post: 02-19-2009, 10:37 PM -
Converting to ASCII and vice-versa
By pheonix in forum New To JavaReplies: 2Last Post: 09-09-2008, 04:43 AM -
Document conversion PDF to MS doc and vice versa
By abintoms in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks