1

I wrote the following code but similar characters are always in the same case. What's wrong in this code and How can this problem be solved??

private void genBTActionPerformed(java.awt.event.ActionEvent evt) {                                      
    String str = new String(strTF.getText());
    int n = str.length();
    char ch;
    int i;
    for(i = 0; i < n; i++) {
        if(i % 2 == 0) {
            ch = Character.toLowerCase(str.charAt(i));
            str = str.replace(str.charAt(i), ch);
        } else {
            ch = Character.toUpperCase(str.charAt(i));
            str = str.replace(str.charAt(i), ch);
        }
    }
    jumTF.setText(str);
}   
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sana
  • 23
  • 1
  • 1
  • 3
  • `replace` works on the whole String. – Maroun Jan 28 '15 at 13:22
  • `replace(oldChar, newChar)` returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. What you want is to [replace a character at a specific location](http://stackoverflow.com/questions/6952363/java-replace-a-character-at-a-specific-index-in-a-string). – Reti43 Jan 28 '15 at 13:24
  • You should work out things like this in a command line app. before worrying about how to get them to work in a GUI. And on that note, 'changing case in a string' has nothing to do with either Swing or your IDE. – Andrew Thompson Jan 28 '15 at 13:32

3 Answers3

6

Unlike what its name says, .replace() replaces characters/CharSequences in the whole input. The difference with .replaceAll() is that it takes literals as arguments and not regexes/regex replacements strings (and that it has an overload taking two chars as arguments). That is the second worst misnamed method of the String class after matches().

Moreover you create a new String on each character you replace, so you have n+1 strings for a n character long string. Do it like this instead:

final char[] chars = str.toCharArray();

final int len = chars.length;

char c;

for (int i = 0; i < len; i++) {
    c = chars[i];

    chars[i] = i % 2 == 0
        ? Character.toLowerCase(c)
        : Character.toUpperCase(c);
}

jumTF.setText(new String(chars));
fge
  • 119,121
  • 33
  • 254
  • 329
0

In your program you were using replace() which replaces characters/CharSequences in the whole input what you need to do is

  • Put the string into an array.
  • Iterate over said array.
  • convert that array back into string

    private void genBTActionPerformed(java.awt.event.ActionEvent evt) {

    String str = new String(strTF.getText());
    char [] chr= str.toCharArray();
    int n = chr.length;
    char ch;
    int i;
    for(i = 0; i < n; i++) {
        if(i % 2 == 0) {
            ch = Character.toLowerCase(chr[i]);
           chr[i]=ch;
        } else {
            ch = Character.toUpperCase(chr[i]);
         chr[i]=ch;
        }
    }
    jumTF.setText(new String(chr)); }
    

hope this will help you :)

0

Since String are immutable in java , you can use StringBuilder or StringBuffer to solve this problem

StringBuilder str=new StringBuilder(inputString);

You can use your own logic just with slight change instead of using

str = str.replace(str.charAt(i), ch);//since  it replaces in whole string

Use

str.setCharAt(i,ch);

So your final Program looks like this :

for(i = 0; i < n; i++) {
        if(i % 2 == 0) {
            ch = Character.toLowerCase(str.charAt(i));
            str.setCharAt(i,ch);
        } else {
            ch = Character.toUpperCase(str.charAt(i));
            str.setCharAt(i,ch);
        }
    }

Suppose InputString is : stackoverflow then output is : sTaCkOvErFlOw

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62