how to convert Lower case letters to upper case letters & and upper case letters to lower case letters
Solution 1
setText
is changing the text content to exactly what you give it, not appending it.
Convert the String
from the field first, then apply it directly...
String value = "This Is A Test";
StringBuilder sb = new StringBuilder(value);
for (int index = 0; index < sb.length(); index++) {
char c = sb.charAt(index);
if (Character.isLowerCase(c)) {
sb.setCharAt(index, Character.toUpperCase(c));
} else {
sb.setCharAt(index, Character.toLowerCase(c));
}
}
SecondTextField.setText(sb.toString());
Solution 2
You don't have to track whether you've already changed the character from upper to lower. Your code is already doing that since it's basically:
1 for each character x:
2 if x is uppercase:
3 convert x to lowercase
4 else:
5 if x is lowercase:
6 convert x to uppercase.
The fact that you have that else
in there (on line 4) means that a character that was initially uppercase will never be checked in the second if
statement (on line 5).
Example, start with A
. Because that's uppercase, it will be converted to lowercase on line
3 and then you'll go back up to line 1 for the next character.
If you start with z
, the if
on line 2 will send you directly to line 5 where it will be converted to uppercase. Anything that's neither upper nor lowercase will fail both if
statements and therefore remain untouched.
Solution 3
You can use StringUtils.swapCase()
from org.apache.commons
Solution 4
This is a better method :-
void main()throws IOException
{
System.out.println("Enter sentence");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String sentence = "";
for(int i=0;i<str.length();i++)
{
if(Character.isUpperCase(str.charAt(i))==true)
{
char ch2= (char)(str.charAt(i)+32);
sentence = sentence + ch2;
}
else if(Character.isLowerCase(str.charAt(i))==true)
{
char ch2= (char)(str.charAt(i)-32);
sentence = sentence + ch2;
}
else
sentence= sentence + str.charAt(i);
}
System.out.println(sentence);
}
Solution 5
The problem is that you are trying to set the value of SecondTextField
after checking every single character in the original string. You should do the conversion "on the side", one character at a time, and only then set the result into the SecondTextField
.
As you go through the original string, start composing the output from an empty string. Keep appending the character in the opposite case until you run out of characters. Once the output is ready, set it into SecondTextField
.
You can make an output a String
, set it to an empty string ""
, and append characters to it as you go. This will work, but that is an inefficient approach. A better approach would be using a StringBuilder
class, which lets you change the string without throwing away the whole thing.
akki0996
Updated on May 24, 2020Comments
-
akki0996 almost 4 years
Alternately display any text that is typed in the textbox
// in either Capital or lowercase depending on the original // letter changed. For example: CoMpUtEr will convert to // cOmPuTeR and vice versa. Switch.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e ) String characters = (SecondTextField.getText()); //String to read the user input int length = characters.length(); //change the string characters to length for(int i = 0; i < length; i++) //to check the characters of string.. { char character = characters.charAt(i); if(Character.isUpperCase(character)) { SecondTextField.setText("" + characters.toLowerCase()); } else if(Character.isLowerCase(character)) { SecondTextField.setText("" + characters.toUpperCase()); //problem is here, how can i track the character which i already change above, means lowerCase** } }} });
-
Luiggi Mendoza about 11 yearsYou were faster than me =\, still +1
-
MadProgrammer about 11 years@LuiggiMendoza I was writing while you were editing the question ;)
-
Nazik about 10 yearsPlease don't simply post the code. Give some explanation or information or usage about your code. For example, see this answer.
-
Lupin about 9 yearsHi Umarjon, on future answers try to add a short explanation on your answer that will help the user that posted the question to understand your solution and learn from at
-
Toby Speight about 7 yearsWhilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how and why this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
-
Noel Widmer over 6 yearsCan you elaborate on your answer? Posting a piece of code without any text is often not very helpful.
-
Lovepreet Singh almost 6 yearsAlso provide some explanation to it.
-
yunandtidus almost 6 yearsThis does not answer the issue