omar ahmed
omar ahmed

Reputation: 1

Removing Characters within a string-Java

I keep getting an error with removing a character from within a string. I have tried everything that i could find on this site and nothing has worked. This is NOT a help post. Rather maybe an answer that explains why this shows up and how to fix it in case someone else encounters this issue. Without further a due, here is my code:

public JTextField Clean()
{
    String Cleaner = TopField.getText();
    Cleaner=Cleaner.toLowerCase();
    int Length = Cleaner.length();
    StringBuilder Combiner = new StringBuilder(Cleaner);
    for (int x=0;x+1<Length;x++)
    {
       char c = Cleaner.charAt(x);
       char c1 = Cleaner.charAt(x+1);
       if(c==' ' && c1==' ')
        {
            Combiner.deleteCharAt(x);
            Cleaner=Combiner.toString();
        }
       if(c!='a' && c=='b' && c!='c' && c!='d' && c!='f' && c!='g' && c!='h' && c!='i' && c!='j' && c!='k' && c!='l' && c!='m' && c!='n' && c!='o' && c!='p' && c!='q' && c!='r' && c!='s' && c!='t' && c!='u' && c!='v' && c!='w' && c!='x' && c!='y' && c!='z' && c!=' ')
        {Combiner.deleteCharAt(x);
         Cleaner=Combiner.toString();}
       }     

      TopField.setText(Cleaner); 
      return TopField;               
}

I receive an error that states that My value is out of bounds by the length of the string that i input. Please note that this is a method inside a class that i created that removes any character that is not an alphabet or space.

Thanks in advance

Upvotes: 0

Views: 3992

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347334

There are a number of things that pop out at me.

  • Your basing your loop on a fixed value (Length), but where the actual length of the String can decrease...
  • You are potentially removing 2 characters per loop (there are two deleteCharAt calls)
  • The loop doesn't take into account the shrinking size of the String. For example. x == 1, you remove the character at x, you increment x by 1 (x == 2), effectively skipping a character (the character at position 2 is now at position 1
  • Your if statement is unnecessarily long. In fact, depending on your needs, you could use Character.isDigit or Character.isLetter and Character.isWhiteSpace

String Cleaner = TopField.getText();
Cleaner = Cleaner.toLowerCase();
StringBuilder Combiner = new StringBuilder(Cleaner);
int x =0;
while (x < Combiner.length()) {
    char c = Combiner.charAt(x);
    if (c >= 'a' && c <= 'z' || c == ' ') {
        Combiner.deleteCharAt(x);
    } else {
        x++;
    }
}

From the looks of your code, you appear to wanting to filter a JTextField so it will only allow numeric values. It would be much better to use something like a JSpinner, JFormattedTextField or DocumentFilter and ensure the correctness of the data as it's entered...IMHO

Upvotes: 2

ram
ram

Reputation: 1

I used a isDigit() function and found the output as incorrect. Look at the code I tested and found problem with the output. Any one explain.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String temp="you got 211111 out of 211111?";
    StringBuilder cleaner=new StringBuilder(temp);
    for(int i=0;i<cleaner.length();i++)
    {
        char c=cleaner.charAt(i);
        if(Character.isDigit(c))
        {
            cleaner.deleteCharAt(i);
        }
    }
    System.out.println(cleaner);

I am getting output as : you got 111 out of 111?

it is not removing some digits.

Also found that no function called replaceAll() is there in Java.

Upvotes: 0

Simon MᶜKenzie
Simon MᶜKenzie

Reputation: 8694

As you remove characters, Cleaner becomes shorter, so you're likely to reach a point where x is too large.

I would suggest a different approach using regular expressions:

string cleaned = TopField.getText().toLowerCase().replaceAll("[^a-z ]", "");

Upvotes: 4

Related Questions