-1

This code Works Fine for test .. but the moment it hit repetition of number this doesn't give valid output..

For Example for this**(ancxyz123, 3,5, XYZ)** input i'm getting ancXYZ123 as Output not ancXYZ1235 which was supposed to be the output..

this is about checking the string and printing it without any special character and duplicates characters and if any character is present in Uppercase in the whole string it should print that Character in uppercase..

public class Test1222015 {

public static void main(String... strings) {

    String s = "ancxyz123, 3,5, XYZ";

    String s1 = StringTest(s);
    System.out.println(s1);
}

public static String StringTest(String s) {
    // TODO Auto-generated method stub
    int sLength = s.length();
    StringBuilder sb = new StringBuilder();
    boolean found = false;
    boolean previously = false;
    boolean uppercase = false;
    if (s != null && sLength > 1) {
        for (int i = 0; i < sLength; i++) {
            char temp = s.charAt(i);

            if (Character.toString(temp).matches("[a-zA-Z\\d\\s]")) {

                if (Character.isAlphabetic(temp)) {
                    for (int j = 0; j < i; j++) {
                        char tempinner = s.charAt(j);
                        if (Character.isAlphabetic(tempinner)) {
                            if (Character.toLowerCase(temp) == Character
                                    .toLowerCase(tempinner)) {
                                previously = true;
                                break;
                            }
                        }
                    }
                    for (int j = i + 1; j < sLength; j++) {
                        char tempinner = s.charAt(j);
                        if (Character.isAlphabetic(tempinner)) {
                            if (Character.toLowerCase(temp) == Character
                                    .toLowerCase(tempinner)) {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!previously && !found) {
                        sb.append(temp);
                    }

                    else if (!previously && found) {
                        for (int j = i + 1; j < sLength; j++) {
                            char assignCheck = s.charAt(j);
                            if (Character.isAlphabetic(assignCheck)) {
                                if (Character.toLowerCase(temp) == Character
                                        .toLowerCase(assignCheck)) {
                                    uppercase = Character
                                            .isUpperCase(assignCheck);
                                    if (uppercase) {
                                        break;
                                    }
                                }
                            }

                        }
                        if (uppercase) {
                            sb.append(Character.toUpperCase(temp));
                        } else {
                            sb.append(temp);
                        }

                    }

                } // alpha if ends here
                else if (Character.isDigit(temp)) {
                    System.out.println(temp);
                    for (int j = 0; j < i; j++) {
                        char tempinner = s.charAt(j);
                        if (Character.isDigit(tempinner)) {
                            if (temp == tempinner) {
                                previously = true;
                                break;
                            }
                        }
                    }

                    if (!previously) {
                        sb.append(temp);
                    }

                     /* this edited Piece Made it Work */
                        previously=false;

                }

                else {
                    sb.append(" ");
                }

            }// test condition if ends here

            // ends outer for loop
        }

    }// ends outer if condition
    return sb.toString();
}

}

please also suggest me some good way to do this work without using so much of loops and conditions

Thanks :) Edited: **

  • Its working Now

**

But Please Suggest Some GOOD way To do This Work and Avoid so much of loops and all

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ankur Anand
  • 3,873
  • 2
  • 23
  • 44
  • You might be interested in the [string.indexOf](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf%28int%29) function. Or, better yet, [StringBuilder.indexOf](http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#indexOf(java.lang.String,%20int)). Either of those will prevent you from having to write a loop to see if the current character was found previously. – Jim Mischel Feb 13 '15 at 17:42

1 Answers1

0

You might find the following answers in StackOverflow helpful. I saw two type of solutions. One uses regular expressions, the other uses StringBuffer.

Community
  • 1
  • 1
Ely
  • 10,860
  • 4
  • 43
  • 64