Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default Help on Printing the IDs of a list

    Hi,

    I have a list consisting of some elements and each element has an Id or a number within it, how can I go about printing those ids in order and to the following format assuming the first element has id of 1, second has id of 2, third has id of 3 and so on and with commas in between each id.

    1, 2, 3, ..

    I am thinking of the following loop but it doesn't work for me.
    Java Code:
    while(list.get(1).getId() <= 5)
    {System.out.println(list.get(Id - 1).getId());}
    Sorry, I have changed my code a bit to make it more understandable. If it doesn't make sense please advise how I should go about this problem based on what I mentioned above.
    Thanks.
    Last edited by jboy; 09-10-2009 at 11:57 AM.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    844
    Rep Power
    6

    Default

    Hi,
    You give the complete piece of code with clear description.Really ,its not understandable.People can't give solutions with assumptions.

    -Regards
    Ramya
    Ramya:cool:

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Just initialize a StringBuilder then do
    Java Code:
    for(Criminal criminal: criminals) {
           sb.append(criminal.getId()+", ");
    }
    Then when the loop is done just chop off the extra space comma from the end of the StringBuilder.

  4. #4
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Can I just ask what sb and append are and what do they do?
    Can't I just go and print the ids of each element?

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    sb would be the StringBuilder that I said you should initialize.
    The methods for the StringBuilder (including those that delete characters) are well documented in the API specs for that class.

  6. #6
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    how can i use this object of class to print out the IDs?

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    I know that one, it's

    Java Code:
    System.out.println(sb.toString());

    r035198x(<-----rocket scientist

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Quote Originally Posted by jboy View Post
    how can i use this object of class to print out the IDs?
    As already suggested, read the API for StringBuilder and it should all come clear.

  9. #9
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Thanks everybody, it is working for me now but I need to get rid of the extra comma in the end list. And I don't know how to do it. For example, I need to have 1, 2, 3 and it is showing this as 1, 2, 3,
    I am still using the stringbuilder class.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  11. #11
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    844
    Rep Power
    6

    Default

    to remove the end comma ,use substring method.

    gothru this below
    substring
    public String substring(int start,
    int end)Returns a new String that contains a subsequence of characters currently contained in this sequence. The substring begins at the specified start and extends to the character at index end - 1.

    Parameters:
    start - The beginning index, inclusive.
    end - The ending index, exclusive.
    Returns:
    The new string.
    Throws:
    StringIndexOutOfBoundsException - if start or end are negative or greater than length(), or start is greater than end.
    Ramya:cool:

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    to remove the end comma ,use substring method.

    ...
    They are using StringBuilder not string and they don't need substring for that.
    Read the other posts already made in this thread so far.

  13. #13
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    844
    Rep Power
    6

    Default

    Hello Sir r035198x,

    Please gothru the api.

    StringBuilder (Java 2 Platform SE 5.0)

    -Regards
    Ramya
    Ramya:cool:

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hello Sir r035198x,

    Please gothru the api.

    StringBuilder (Java 2 Platform SE 5.0)

    -Regards
    Ramya
    Why should I go through the API? I know what methods are in there and I wouldn't pick substring for deleting the character at the last position. Better let the OP read the API specs as has already been suggested multiple times rather than trying to spoon feed them.

    BTW latest Java version is 1.6 these days.

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    844
    Rep Power
    6

    Default

    Quote Originally Posted by jboy View Post
    Thanks everybody, it is working for me now but I need to get rid of the extra comma in the end list. And I don't know how to do it. For example, I need to have 1, 2, 3 and it is showing this as 1, 2, 3,
    I am still using the stringbuilder class.
    I have given the reply for this..Iam not arguing with you which is latest in java.
    Ramya:cool:

  16. #16
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    I have given the reply for this..Iam not arguing with you which is latest in java.
    Fuburable already answered that question and I had already told the OP the same thing, that they must read the API. There is no need for anyone to copy and paste the contents of the API here.

  17. #17
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Thank you for the information, I found the following method and I put it before after the toString method but it didn't delete the last comma and I don't know why.
    Java Code:
    System.out.println(sb.toString());
    sb.deleteCharAt(sb.length() - 1);
    Thanks.

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Because you printed out the string first and then removed the comma?

  19. #19
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    I tried swapping those lines around but still wasn't able to get rid of the comma.

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Verify that the String is indeed at the position that you are passing. Don't you have a space at the end?

Page 1 of 2 12 LastLast

Similar Threads

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 04:40 PM
  2. Revised Linked List printing method question
    By CirKuT in forum New To Java
    Replies: 7
    Last Post: 12-12-2008, 11:21 PM
  3. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM
  4. How to access ArrayList in List of List?
    By alvations in forum New To Java
    Replies: 5
    Last Post: 10-08-2008, 01:23 PM
  5. Printing Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:41 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
  •