Results 1 to 3 of 3
Thread: Remove comma
- 02-04-2011, 06:18 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 8
- Rep Power
- 0
Remove comma
I will be getting input from the user and will print the results to a file. The file is like this:
123,
45,
82,
23,
12,
I want to remove the comma of the last input number, which is in this case is number 12. So i want it to be like this:
123,
45,
82,
23,
12
How do i remove the comma? Thank you
- 02-04-2011, 06:20 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Try using an if clause which tests if it's the last item being put into the file, then just print the number, else print the number with a comma.
- 02-04-2011, 07:58 PM #3
an other approach is to loop through your collection only to the length - 1 and adding the comma and outside the loop print only the last number without the comma. look at the following example:
Java Code:public class CommaExample { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4 }; int i = 0; for (i = 0; i < arr.length - 1; i++) { System.out.println(arr[i] + ","); } System.out.println(i + 1); } }
Similar Threads
-
Formatting numbers or decimals (around comma)
By Joris in forum Advanced JavaReplies: 1Last Post: 04-22-2010, 05:46 PM -
Delimite the file using comma
By gokulcool in forum New To JavaReplies: 3Last Post: 12-30-2008, 05:40 PM -
How to parse the CSV(Comma separation values)file and validate the file using java
By padmajap13 in forum Advanced JavaReplies: 7Last Post: 05-23-2008, 03:46 AM -
A utility class that parses a Comma Separated Values (CSV) file
By Java Tip in forum java.ioReplies: 0Last Post: 04-16-2008, 10:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks