Results 1 to 20 of 23
- 09-10-2009, 09:44 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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.
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.Java Code:while(list.get(1).getId() <= 5) {System.out.println(list.get(Id - 1).getId());}
Thanks.Last edited by jboy; 09-10-2009 at 10:57 AM.
- 09-10-2009, 09:47 AM #2
Hi,
You give the complete piece of code with clear description.Really ,its not understandable.People can't give solutions with assumptions.
-Regards
RamyaRamya:cool:
- 09-10-2009, 09:48 AM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Just initialize a StringBuilder then do
Then when the loop is done just chop off the extra space comma from the end of the StringBuilder.Java Code:for(Criminal criminal: criminals) { sb.append(criminal.getId()+", "); }
- 09-10-2009, 11:13 AM #4
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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?
- 09-10-2009, 11:19 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 09-10-2009, 11:52 AM #6
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
how can i use this object of class to print out the IDs?
- 09-10-2009, 11:55 AM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
I know that one, it's
Java Code:System.out.println(sb.toString());
r035198x(<-----rocket scientist
- 09-10-2009, 12:13 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
As already suggested, read the API for StringBuilder and it should all come clear.
- 09-11-2009, 02:16 AM #9
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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.
-
The API has a method for that. Again: please read the API first.
- 09-11-2009, 10:05 AM #11
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:
- 09-11-2009, 10:16 AM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-11-2009, 10:58 AM #13
Hello Sir r035198x,
Please gothru the api.
StringBuilder (Java 2 Platform SE 5.0)
-Regards
RamyaRamya:cool:
- 09-11-2009, 11:11 AM #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 09-11-2009, 11:13 AM #15
- 09-11-2009, 11:22 AM #16
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-11-2009, 01:29 PM #17
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
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.
Thanks.Java Code:System.out.println(sb.toString()); sb.deleteCharAt(sb.length() - 1);
- 09-11-2009, 01:35 PM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Because you printed out the string first and then removed the comma?
- 09-11-2009, 02:04 PM #19
Member
- Join Date
- Aug 2009
- Posts
- 56
- Rep Power
- 0
I tried swapping those lines around but still wasn't able to get rid of the comma.
- 09-11-2009, 02:10 PM #20
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Help in Printing
By kirly in forum Advanced JavaReplies: 3Last Post: 10-03-2011, 03:40 PM -
Revised Linked List printing method question
By CirKuT in forum New To JavaReplies: 7Last Post: 12-12-2008, 10:21 PM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 08:53 PM -
How to access ArrayList in List of List?
By alvations in forum New To JavaReplies: 5Last Post: 10-08-2008, 12:23 PM -
Printing Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks