Results 1 to 3 of 3

Thread: Remove comma

  1. #1
    phantom06 is offline Member
    Join Date
    Nov 2010
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    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

  1. Formatting numbers or decimals (around comma)
    By Joris in forum Advanced Java
    Replies: 1
    Last Post: 04-22-2010, 05:46 PM
  2. Delimite the file using comma
    By gokulcool in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 05:40 PM
  3. Replies: 7
    Last Post: 05-23-2008, 03:46 AM
  4. Replies: 0
    Last Post: 04-16-2008, 10:58 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
  •