mardi 26 avril 2016

Calculate total votes using data from files

I am a beginner coder and don't have much experience in java. I am confused about a program that I have to code using arrays, if-else statements, and loops.

The assignment prompt is, "Write a program that uses the file ElectionVotes.txt, which contains state names and electoral votes, and the file ElectionData.txt, which contains state names and total votes for each candidate, to calculate the number of electoral votes earned by Obama and Romney in the 2012 election. Your program should build a lookup table with the state names, another table with the associated electoral votes for that state. The output should show two numbers - the total electoral votes for each candidate."

I have tried to do it, but I don't know if this will work.

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

public class Electoral_Votes {

public static void main(String[] args) {


   String str, filename, filename2;
     double[] States = new double[51];
     int[] ObamaVote = new int[51];
     int[] RomneyVote = new int[51];
     int high, low, high2, low2, EV;
     double Total, sum = 0;
     int i = 0;
   Scanner keyboard = new Scanner (System.in);

    try{
         Scanner inputFile = new Scanner(new File("ElectionVotes.txt"));
         str = inputFile.nextLine();

         while (inputFile.hasNext())
         {
             str = inputFile.next();
             States[i] = inputFile.nextDouble();
             EV = inputFile.nextInt();


              for(i = 0; i <= 51; i++)
         {
             RomneyVote = getRomneyV();
             ObamaVote = getObamaV();
             if ( RomneyVote > ObamaVote )
             {
                RomneyVote[i]+=EV;



              }

         }
         }

     }catch (IOException e)
     {
         System.out.println("File not found");
     }


}


    public static double[] getElectionData ()        
    {
         double[] States = new double[51];
         int[] ObamaVote = new int[51];
         int[] RomneyVote = new int[51];
         int i = 0;
  try{

         Scanner inputFile = new Scanner(new File("ElectionData.txt"));
         String str = inputFile.nextLine();

         while (inputFile.hasNext())
         {
             str = inputFile.next();
             States[i] = inputFile.nextDouble();
             ObamaVote[i] = inputFile.nextInt();
             RomneyVote[i] = inputFile.nextInt();
         }
     }catch (IOException e)
     {
         System.out.println("File not found");
     }

return States;
}

public static int[] getRomneyV ()        
{
     double[] States = new double[51];
     int[] ObamaVote = new int[51];
     int[] RomneyVote = new int[51];
     int i = 0;
 try{

         Scanner inputFile = new Scanner(new File("ElectionData.txt"));
         String str = inputFile.nextLine();

         while (inputFile.hasNext())
         {
             str = inputFile.next();
             States[i] = inputFile.nextDouble();
             ObamaVote[i] = inputFile.nextInt();
             RomneyVote[i] = inputFile.nextInt();

         }
     }catch (IOException e)
     {
         System.out.println("File not found");
     }

return RomneyVote;

}

public static int[] getObamaV ()        
{
     double[] States = new double[51];
     int[] ObamaVote = new int[51];
     int[] RomneyVote = new int[51];
     int i = 0;
try{

         Scanner inputFile = new Scanner(new File("ElectionData.txt"));
         String str = inputFile.nextLine();

         while (inputFile.hasNext())
         {
             str = inputFile.next();
             States[i] = inputFile.nextDouble();
             ObamaVote[i] = inputFile.nextInt();
             RomneyVote[i] = inputFile.nextInt();
         }
     }catch (IOException e)
     {
         System.out.println("File not found");
     }

return ObamaVote;
}

}

Aucun commentaire:

Enregistrer un commentaire