Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: File not found exception

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default File not found exception

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: File not found exception

    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: File not found exception

    I put it in the original post.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: File not found exception

    Error: Unhandled exception type java.io.FileNotFoundException
    You need to put those lines of code inside of a try{}catch() block for the exception that can be thrown by the code on the line numbers given in the error messages.
    Be sure to add a call to the printStackTrace() method inside the catch block.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: File not found exception

    I am very new to java and do not know what those do or anything. Can you give me a quick rundown?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: File not found exception

    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: File not found exception

    When I put in a try catch block it then gives me the error that my PrintWriter variables cannot be resolved.
    try
          {
            PrintWriter modifyInElection = new PrintWriter("inElection.txt");
            PrintWriter outElection = new PrintWriter("outElection.txt");
          }
          catch(FileNotFoundException d)
          {
            System.out.println("FileNotFoundException");
          }
    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;
          }

    And Error:
    4 errors found:
    --------------
    *** Errors ***
    --------------
    File: C:\Users\Forrest\Desktop\Games\MyStuff\School\CS11 0\Election.java [line: 62]
    Error: modifyInElection cannot be resolved
    File: C:\Users\Forrest\Desktop\Games\MyStuff\School\CS11 0\Election.java [line: 66]
    Error: modifyInElection cannot be resolved
    File: C:\Users\Forrest\Desktop\Games\MyStuff\School\CS11 0\Election.java [line: 102]
    Error: outElection cannot be resolved
    File: C:\Users\Forrest\Desktop\Games\MyStuff\School\CS11 0\Election.java [line: 104]
    Error: outElection cannot be resolved

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: File not found exception

    Move the definition of the variables OUTSIDE of the {} in the try{}catch block.
    Assign them values inside the {}s.

    Or move the catch() below the last place the variables are used.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: File not found exception

    I'm still not sure what you mean. I'm very new to java and do not know what you mean.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: File not found exception

    Did you try moving the }catch( ... to the end of the code that uses the file IO classes?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2013
    Location
    Morgantown
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: File not found exception

    I'm mostly confused on what to put inside the try block.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: File not found exception

    Put all the calls to constructors and methods that can throw an exception. The compiler will give you an error message if you don't put a statement inside that should be inside.
    The simplest way would be to put ALL of the statements in the method inside the {}s of the try.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    fpritt24 (November 24th, 2013)

Similar Threads

  1. File not found
    By radulescuiulia in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 29th, 2013, 06:00 PM
  2. Replies: 0
    Last Post: March 20th, 2012, 10:46 AM
  3. .java File not found
    By MeteoricDragon in forum What's Wrong With My Code?
    Replies: 18
    Last Post: November 17th, 2011, 11:53 PM
  4. File not found exception
    By mwr76 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 10th, 2011, 03:54 PM
  5. no data found ms -access sql exception
    By jatinrai199 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 16th, 2011, 03:22 PM