Results 1 to 8 of 8
  1. #1
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default [SOLVED] String Length issue.

    Hello

    I have yet another question which will be very basic to you guys. :D

    I have tried searching the forums and google for the answer but i really can't find it.
    I'll give you an example of what i want to do.

    Java Code:
    String Name = "Chris";
    int NameLen = Name.length;
    
    System.out.println(Name + "\t" + Channel);
    In between the two +'s with the \t i want to add the length of the string in as extra space so the columns line up properly when displayed.

    On VB i remember it used to be len(VALUE) but i have no idea in java and can't find it. lol

    Thanks

    --
    Chris

  2. #2
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    See the documentation for printf()
    Formatting out

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    10

    Default

    System.out.println(name + "\t" + name.length() + "\t" + channel);

    Don't capitalize the first letter of field names: thisIsAFieldName

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    9

    Default

    See String.format() too.

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    10

    Default

    Raffaele and Neil both gave good suggestions. If you take some time, Java can do a great deal of formatting with a small amount of weird-looking code (comes from the C background) ;-)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

    Default

    Quote Originally Posted by Steve11235 View Post
    System.out.println(name + "\t" + name.length() + "\t" + channel);

    Don't capitalize the first letter of field names: thisIsAFieldName
    I think he wants to added spaces equal number of the String length, name. I think he has to use formating. Or else he can take a general approach using print(). But I don't think it's an effective way to work on.

    Nice suggestion. ;)

  7. #7
    ChrisMayhew is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Thanks guys, i will have a look at doing this once i have done the bigger jobs. Leave the little ones to last.. hehe

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    22

Similar Threads

  1. what does num.length method does?
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 10:19 PM
  2. queue length of Socket
    By hemant in forum Networking
    Replies: 6
    Last Post: 07-13-2008, 06:28 PM
  3. Integer length
    By jithan in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 03:35 PM
  4. Replies: 2
    Last Post: 03-25-2008, 06:49 AM
  5. Help with method length
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:29 PM

Posting Permissions

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