Hey,
I was wondering if you guys could help me with a homework problem that I have. I need to input data in from a text file and then put that data into two separate arrays. I believe I have that somewhat done now, but I will need to manipulate the data and add a third column to the printout. Any help would be greatly appreciated. I am looking to figure out what needs to be done next and if my current code looks correct.
The text file is in two columns: name vote
import java.io.*;
import java.util.*;
public class Array
{
public static void main(String[] args) throws FileNotFoundException
{
String nameFileIn;
int candidates;
int numCand = 0;
Scanner scan = new Scanner(System.in);
//Get File Name from user
System.out.print("Please enter the name of the input file: ");
nameFileIn = scan.next();
Scanner fileScan = new Scanner(new FileReader(nameFileIn));
//Get the number of candidates in the file with a loop to validate input
do
{
System.out.print("How many candidates are in the file: ");
candidates = scan.nextInt();
}
while (candidates < 1);
String[] candidateLastName = new String[candidates];
int[] candidateVoteTotal = new int[candidates];
while (fileScan.hasNext() && numCand < candidates)
{
candidateLastName[numCand] = fileScan.next();
candidateVoteTotal[numCand] = fileScan.nextInt();
numCand++;
}
System.out.println();
System.out.println("The election results are: ");
System.out.printf("%s%20s%20s%n", "Candidate", "" +
"Votes Received", "% of Total Votes");
}
}