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

Thread: help with printing output in columns

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

    Default help with printing output in columns

    I have an assignment that I have completed the code but I am having problems formatting the output into columns. I know I should use printf, but I am getting an error message with my arguments. I am just begining to learn Java. Thanks.

    import java.util.*;
    import java.text.*;
    import java.io.*;

    public class election {

    public static void main(String[] args) {

    Scanner console = new Scanner(System.in);

    String [] candidates = new String [5];
    int [] votes = new int [5];
    int totalVotes = 0;
    double perOfVotes = 0.00;

    for(int num= 0; num < 5; num++)
    {
    System.out.println("Enter the name of candidate "+ (num+1) +": ");
    candidates[num] = console.next();
    System.out.println("Enter the total votes for candidate "+ (num+1)+": ");
    votes[num] = console.nextInt();
    totalVotes = totalVotes + votes[num];
    }

    System.out.println("Candidate\t" + "Votes Received\t" + "% of Total Votes");
    for(int counter=0; counter < candidates.length; counter++)
    {
    perOfVotes = (double)votes[counter]/totalVotes*100;

    DecimalFormat df = new DecimalFormat("#.##");

    System.out.printf ("%5s%5d%5d %n", candidates[counter] + votes[counter] + (df.format(perOfVotes)));
    }

    System.out.printf("%5d", "Total" + totalVotes);
    }
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: help with printing output in columns

    Quote Originally Posted by jblankinship View Post
    I have an assignment that I have completed the code
    Great!

    Quote Originally Posted by jblankinship View Post
    but I am having problems formatting the output into columns.
    Oh. So you haven't completed all of the code. (In my opinion, the code is not "complete" until it runs and behaves the way that you expect.) But having code that compiles with no errors and starts to run before bailing out is a good start. A very good start.

    Quote Originally Posted by jblankinship View Post
    I am getting an error message with my arguments.
    If you will post the exact, complete, non-paraphrased, non-abbreviated error message(s) you are getting and tell us what input(s) you gave the program and if you will point out to us the specific line that is flagged as causing the error, maybe someone can walk you through the steps to learning how to use the error messages to help you debug a situation like this.

    Quote Originally Posted by jblankinship View Post
    I am just begining to learn Java
    Well, no one was born knowing this stuff, you know. So: Let's get the show on the road!


    Cheers!

    Z

Similar Threads

  1. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  2. JList with multiple columns
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: January 27th, 2011, 08:44 AM
  3. [SOLVED] Tabbing columns
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 24th, 2011, 10:50 PM
  4. need help Printing output from a JTextfield
    By juanbond311 in forum Java Theory & Questions
    Replies: 27
    Last Post: June 21st, 2010, 08:26 AM
  5. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM