SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace, trim, and output string - not working

    Code:
    		
    String sentence;
    char[] badChars = { '!', ':', ';', '$', '#', '@', '%', '^', '&', '*', '(', ')', '-', '=', '+', '{', '}', '[', ']', '/', '\\' };
    ConsoleIO console = new ConsoleIO();
    System.out.print("Enter a sentence: ");
    sentence = console.readLine();
    int sentenceLength = sentence.length();
    for (int j = 0; j < Array.getLength(badChars); j++) {
    	sentence.replace(badChars[j], ' ');
    }
    sentence.trim(); 
    System.out.println(sentence);
    the output is the same as the input - no change..
    can someone please tell me why?

  2. #2
    Non-Member
    Join Date
    Aug 2006
    Location
    Kansas City
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    String sentence;
    char[] badChars = { '!', ':', ';', '$', '#', '@', '%', '^', '&', '*', '(', ')', '-', '=', '+', '{', '}', '[', ']', '/', '\\' };
    ConsoleIO console = new ConsoleIO();
    System.out.print("Enter a sentence: ");
    sentence = console.readLine();
    int sentenceLength = sentence.length();
    for (int j = 0; j < sentenceLength; j++) {
    	
      for(i = 0; i < Array.getLength(badChars); i++)
    {
         if(sentence.CharAt(j) == badChars[i])
    {
    sentence.replace(sentence.CharAt(j),' ');
    }
       }
    }
    
    
    }
    sentence.trim(); 
    System.out.println(sentence);
    Hope this helps.
    J
    Last edited by KCChiefs; Oct 8, 2006 at 00:51. Reason: change i to j

  3. #3
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    There's a problem with your code, and a few violations of 'keep it simple' coding practices:

    Array.getLength(badChars)
    badChars is an array, as such it has a length attribute, all you need say is:
    badChars.length
    (note: it is not a method, and doesn't need () )

    sentence.replace(badChars[j], ' ');
    Here is where your code is actually failing. Strings are immutable, which means they cannot be changed, which means that you must assign the result of the operation to a String in order to capture the change:
    sentence= sentence.replace( badChar[j], ' ' );

    Last, you can replace the loop and the badChar array with just one, simple line:
    sentence = sentence.replaceAll( "\\W", " " );
    \W means 'any non-word character', so anything in sentence that is not a-zA-Z_0-9 gets replaced with a space.

    Nice and simple, but it will also remove . and , If you want to keep these, you'll have to replace \\W with a group and specify which characters you want to replace:
    sentence = sentence.replaceAll( "[!:;$#@%^&*()-=+{}/\\[\\]\\\\]", " " );

    [ and ] have a special meaning in regex, and \ is the escape character, so all of these must be escaped, twice. Once for Java, again for regex.

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow thanks!!

    i was actually creating a program to see if a user input sentence is a palindrome, so i want to delete all spaces and illegal chars - your //W thing accomplishes the illegal character hurdle and i have already found a way to delete the spaces

    thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •