// Marty Stepp, CSE 142, Autumn 2008 // Computes who is ahead in 2008 presidential election polls, // based on an input file of states in a format 1 per line such as: // // AK 42 53 3 Oct Ivan Moore Research // // data comes from http://www.electoral-vote.com/ import java.io.*; // for File import java.util.*; // for Scanner public class Election { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("polls.txt")); int obamaVotes = 0; int mccainVotes = 0; while (input.hasNext()) { if (input.hasNextInt()) { int obama = input.nextInt(); int mccain = input.nextInt(); int eVotes = input.nextInt(); if (obama > mccain) { obamaVotes += eVotes; } else if (mccain > obama) { mccainVotes += eVotes; } } else { input.next(); // skip non-integer token } } System.out.println("Obama: " + obamaVotes + " votes"); System.out.println("McCain: " + mccainVotes + " votes"); } }