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???