Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to remove the last comma

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to remove the last comma

    Right now my output is like this:
    2, 4, 6, 8, 10,

    What I want:
    2, 4, 6, 8, 10

    	            PrintWriter fout = new PrintWriter(new BufferedWriter(new FileWriter("numbers.dat"))); 
    	            for(int i = start; i <= 100; i = i + 2)    { 
    	                fout.print(i+","); 
    	            }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to remove the last comma

    Many ways: a) Print the comma out at the start of every loop iteration except the first (use a conditional), or b) print it out at the end of every loop iteration except for the last (use a conditional) c) limit your loop to miss the first or last iteration (depending upon choice) and print those separately (before or after, respectively).

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to remove the last comma

    count=0;
    for ..{
    if (count<4){
    fout.print(i+",");count++;}
    else {fout.print(i);count=0;} 
    }
    try this

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to remove the last comma

    Above code will only work for given set of numbers. What if there are only 3 numbers? 20 Numbers? 10000 numbers?

    It is easier to determine when you are at the first number than the last number. So print a comma before each number except the first.

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to remove the last comma

    Here i will show you the pseudocode.
    PRINT array[FIRST_INDEX];
    LOOP I=1 TO array.LENGTH STEP 1
            PRINT "'";
            PRINT array[I];
    END LOOP

Similar Threads

  1. not able to remove from ArrayList
    By harsha_c in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 03:28 AM
  2. How to remove all Headers from a MimeMessage
    By ArgyrisV in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2011, 12:38 PM
  3. Help Remove file from pad directory
    By georgybaja in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 8th, 2011, 05:29 PM
  4. JTree - Remove All Nodes
    By aussiemcgr in forum Java Theory & Questions
    Replies: 4
    Last Post: December 9th, 2010, 05:27 PM
  5. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM

Tags for this Thread