Alright so I created this program to determine who the winner of an election is that uses a file input/output method and a command line method. I have the program mostly figured out I just need help with this error. I am very new to java and don't understand why this is happening if anyone can help me please.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class Election extends JFrame { String[] lastNames = new String[5]; int[] votesArray = new int[5]; private JButton fileButton,commandButton,exitButton; private fileButtonHandler fbHandler; private commandButtonHandler cbHandler; private exitButtonHandler ebHandler; public Election() { fileButton = new JButton("Write to file method"); fbHandler = new fileButtonHandler(); fileButton.addActionListener(fbHandler); commandButton = new JButton("Use command line method"); cbHandler = new commandButtonHandler(); commandButton.addActionListener(cbHandler); exitButton = new JButton("Exit"); ebHandler = new exitButtonHandler(); exitButton.addActionListener(ebHandler); setTitle("Choose an option"); Container pane = getContentPane(); pane.setLayout(new GridLayout(1,3)); pane.add(fileButton); pane.add(commandButton); pane.add(exitButton); pack(); setVisible(true); } private class fileButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { PrintWriter modifyInElection = new PrintWriter("inElection.txt"); PrintWriter outElection = new PrintWriter("outElection.txt"); Scanner input = new Scanner(System.in); int sumVotes = 0; for(int i = 0;i < 5;i++) { System.out.println("Enter the last name of the " + (i + 1) + " candidate."); String lastName = input.nextLine(); modifyInElection.println(lastName); System.out.println("Enter the number of votes the candidate received."); int vote = input.nextInt(); String dummy = input.nextLine(); modifyInElection.println(vote); sumVotes += vote; } FileReader inElection = new FileReader("inElection.txt"); Scanner inputElection = new Scanner("inElection.txt"); String[] candidate = new String[5]; int[] votes = new int[5]; int i = 0; while(inputElection.hasNextLine()) { candidate[i] = inputElection.nextLine(); votes[i] = inputElection.nextInt(); i++; } double[] votesPercent = new double[5]; for(i = 0;i < 5;i++) votesPercent[i] = votes[i] / sumVotes * 100; int maxValue = 0; for(i = 0;i < 5;i++) { if(votes[i] > maxValue) maxValue = votes[i]; } int j = 0; for(j = 0;j < 5;j++) { if(maxValue == votes[j]) break; } String output = "Candidate Votes Received % of Total Votes"; for(i = 0;i < 5;i++) output = output + candidate[i] + " " + votes[i] + " " + votesPercent[i] + "\n"; output = output + "The Winner of the Election is " + candidate[j]; outElection.println(output); } } private class commandButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { Scanner input = new Scanner(System.in); int sumVotes = 0; for(int i = 0;i < 5;i++) { System.out.println("Enter the last name of the " + (i + 1) + " candidate."); lastNames[i] = input.nextLine(); System.out.println("Enter the number of votes the candidate received."); int votes = input.nextInt(); String dummy = input.nextLine(); votesArray[i] = votes; sumVotes += votes; } double[] votesPercent = new double[5]; for(int i = 0;i < 5;i++) votesPercent[i] = votesArray[i] / sumVotes * 100; int maxValue = 0; for(int i = 0;i < 5;i++) { if(votesArray[i] > maxValue) maxValue = votesArray[i]; } int j = 0; for(j = 0;j < 5;j++) { if(maxValue == votesArray[j]) break; } String output = "Candidate Votes Reveived % of Total Votes\n"; for(int i = 0;i < 5;i++) output = output + lastNames[i] + " " + votesArray[i] + " " + votesPercent[i] + "\n"; output = output + "The Winner of the Election is " + lastNames[j]; System.out.println(output); } } private class exitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main(String[] args) throws IOException { SwingUtilities.invokeLater(new Runnable() { public void run() { new Election(); } }); } }
I get a file not found exception error whenever I compile that appears on the two PrintWriter lines and the FileReader line.
Edit: Here is the error message.
3 errors found:
--------------
*** Errors ***
--------------
File: C:\Users\Forrest\Desktop\Games\MyStuff\School\CS11 0\Election.java [line: 47]
Error: Unhandled exception type java.io.FileNotFoundException
File: C:\Users\Forrest\Desktop\Games\MyStuff\School\CS11 0\Election.java [line: 48]
Error: Unhandled exception type java.io.FileNotFoundException
File: C:\Users\Forrest\Desktop\Games\MyStuff\School\CS11 0\Election.java [line: 62]
Error: Unhandled exception type java.io.FileNotFoundException