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 3 of 3

Thread: Print array with commas

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Print array with commas

    I want to loop through and print each value in the array. I want each int to be separated by commas but I don't want it to end with a comma.

    for (int i=k-1; i<n; i=i+k)
    System.out.printf("%d, ", array[i]);
    System.out.printf("\n");


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Print array with commas

    First of all, make sure that the code after your for declaration is enclosed in brackets (otherwise only the first line of code after the for declaration will be executed every time the for loop iterates).

    To help you more fully, I need to understand why you are using three integers -- i, k, and n -- to scroll through the values of one array... This seems rather messy; could you explain why you are using three integers?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    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: Print array with commas

    Just think a little and you will get what you want. These are not actually the programming problem but your thinking problem. Just do a little brain storming and you will conclude this.

Similar Threads

  1. Replies: 4
    Last Post: December 19th, 2011, 08:57 PM
  2. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 12:32 PM
  3. HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 3rd, 2011, 03:44 PM
  4. No errors but just won't print out the first element of an array list
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 20th, 2011, 06:30 PM
  5. Replies: 1
    Last Post: March 22nd, 2010, 04:34 PM