Thread: Char method

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2

    Char method


    i want to test if username[i] has no char. if it does not than i want to put space in it. but not sure which method to use in ifstatment.

    char username = new char[8];

    for(int i = 0; i < username.length; i++){
    if(username[i] == ){
    username[i] = ' ';
    }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348
    test if username[i] has no char
    The String class has methods that will return the length of the String or say if it is empty.
    want to put space in it
    Use an assignment statement to set the String to a space: " "

    A String object is immutable. Its contents can't be changed. The variable can be assigned to a new String.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Try this:

    Code:
    if(Character.toString(username[i]).equals(null)) 
    { 
    username[i] = ' '; 
    }
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348
    Have you compiled and executed that code? What happened?
    When will the if ever be true? Can you post code that has the if be true?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Acctually I think that there is no need to do that.

    Because I tried this:

    Code:
    char[] username = new char[5]; 
    username[0] = 'a';
    username[1] = 'b'; 
    username[3] = 'd'; 
    username[4] = 'e';
    
    System.out.println(String.copyValueOf(username));
    And it gave the output "ab de"

IMN logo majestic logo threadwatch logo seochat tools logo