Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2009
    Posts
    18
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question Output formatting

    I wrote this code to determine an election winner...I am having problems outputting the answers under their proper heading...This is my code...

    Code:
    import java.util.*;
    import java.io.*;
    
    public class Ch9Prob7
    {
    	//Initialize Scanner
    	static Scanner console = new Scanner(System.in);
    		
    	public static void main(String[] args)
    	{
    		//Initialize and Declare Variables/Arrays
    		String[] nameList = new String[5];
    		double[] nameVotes = new double[5];
    		double totVotes;
    		double[] percent = new double[5];
    	
    		//Introduction and Prompting user for canditates names
    		System.out.println("This program is for entering in 5 names of canditates for an election.\n");
    		System.out.print("Please enter the name of the 5 canditates \n putting a space inbetween the names:");
    
    	//Identify inputted names
    		//First name
    		nameList[0] = console.next();
    		//Second name
    		nameList[1] = console.next();
    		//Third name
    		nameList[2] = console.next();
    		//Fourth name
    		nameList[3] = console.next();
    		//Fifth name
    		nameList[4] = console.next();
    		
    	//User inputting amounnt of votes
    		//The votes for the first person
    		System.out.print("Please enter the amount of votes for " + nameList[0] + ":");
    		nameVotes[0] = console.nextInt();
    	
    		//The votes for the second person
    		System.out.print("Please enter the amount of votes for " + nameList[1] + ":");
    		nameVotes[1] = console.nextInt();
    	
    		//The votes for the third person
    		System.out.print("Please enter the amount of votes for " + nameList[2] + ":");
    		nameVotes[2] = console.nextInt();
    	
    		//The votes for the fourth person
    		System.out.print("Please enter the amount of votes for " + nameList[3] + ":");
    		nameVotes[3] = console.nextInt();
    	
    		//The votes for the fifth person
    		System.out.print("Please enter the amount of votes for " + nameList[4] + ":");
    		nameVotes[4] = console.nextInt();
    	
    	   //Declaring Variables
    		totVotes = nameVotes[0] + nameVotes[1] + nameVotes[2] + nameVotes[3] + nameVotes[4];
    		
    	//Declaring and Calculating percent of total votes
    		
    		//First persons' percent of total votes
    		percent[0] = ((nameVotes[0] / totVotes)* 100.0);
    		//Second persons' percent of total votes
    		percent[1] = ((nameVotes[1] / totVotes)* 100.0);
    		//Third persons' percent of total votes
    		percent[2] = ((nameVotes[2] / totVotes)* 100.0);
    		//Fourth persons' percent of total votes
    		percent[3] = ((nameVotes[3] / totVotes)* 100.0);
    		//Fifth persons' percent of total votes
    		percent[4] = ((nameVotes[4] / totVotes)* 100.0);
    		
    		//Outputting Results
    		System.out.println();
    		System.out.println("Canditate          Votes           % of Votes\n");
    		System.out.printf("\n" + nameList[0] + "             " + nameVotes[0] + "           %.2f", percent[0]);
    		System.out.printf("\n" + nameList[1] + "             " + nameVotes[1] + "           %.2f", percent[1]);
    		System.out.printf("\n" + nameList[2] + "             " + nameVotes[2] + "           %.2f", percent[2]);
    		System.out.printf("\n" + nameList[3] + "             " + nameVotes[3] + "           %.2f", percent[3]);
    		System.out.printf("\n" + nameList[4] + "             " + nameVotes[4] + "           %.2f", percent[4]);
    		System.out.println("\n");
    	
    		//Outputting total amount of votes
    		System.out.print("The total amount of votes is: " + totVotes);
    		System.out.println("\n");	
    		
    	//Determining and outputting winner of election
    	
    		//First person up for election
    		if ((nameVotes[0] > nameVotes[1]) && (nameVotes[0] > nameVotes[2]) && (nameVotes[0] > nameVotes[3]) && (nameVotes[0] > nameVotes[4]))
    			System.out.println("The winner of the election is: " + nameList[0]);
    		
    		//Second person up for election
    		if ((nameVotes[1] > nameVotes[0]) && (nameVotes[1] > nameVotes[2]) && (nameVotes[1] > nameVotes[3]) && (nameVotes[1] > nameVotes[4]))
    			System.out.println("The winner of the election is: " + nameList[1]);
    		
    		//Third person up for election
    		if ((nameVotes[2] > nameVotes[1]) && (nameVotes[2] > nameVotes[0]) && (nameVotes[2] > nameVotes[3]) && (nameVotes[2] > nameVotes[4]))
    			System.out.println("The winner of the election is: " + nameList[2]);
    		
    		//Fourth person up for election
    		if ((nameVotes[3] > nameVotes[1]) && (nameVotes[3] > nameVotes[2]) && (nameVotes[3] > nameVotes[0]) && (nameVotes[3] > nameVotes[4]))
    			System.out.println("The winner of the election is: " + nameList[3]);
    		
    		//Fifth person up for election
    		if ((nameVotes[4] > nameVotes[1]) && (nameVotes[4] > nameVotes[2]) && (nameVotes[4] > nameVotes[3]) && (nameVotes[4] > nameVotes[0]))
    			System.out.println("The winner of the election is: " + nameList[4]);				
    		
    	}
    }
    There is nothing wrong with that code, and here is my output.


    ----jGRASP exec: java Ch9Prob7

    This program is for entering in 5 names of canditates for an election.

    Please enter the name of the 5 canditates
    putting a space inbetween the names:Smith Kline Johnson Doe Java
    Please enter the amount of votes for Smith:200
    Please enter the amount of votes for Kline:150
    Please enter the amount of votes for Johnson:250
    Please enter the amount of votes for Doe:50
    Please enter the amount of votes for Java:350

    Canditate Votes % of Votes


    Smith 200.0 20.00
    Kline 150.0 15.00
    Johnson 250.0 25.00
    Doe 50.0 5.00
    Java 350.0 35.00

    The total amount of votes is: 1000.0

    The winner of the election is: Java

    ----jGRASP: operation complete.

    Why can't I get the number to line up with their corresponding titles??? It isn't putting the numbers in a perfectly lined up order vertically. Any suggestions???

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    have u tried using the printf method?
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    autokustomizer (04-26-2009)

  • #3
    New Coder
    Join Date
    Apr 2009
    Posts
    18
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Yes, I tried with the %-10s, I didn't see any changes in what I was doing... Maybe I am using wrong??

    Code:
    System.out.printf("%-10s \n + nameList[0] ...");
    Is that correct??? I tried that, I didn't see any difference...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •