Results 1 to 15 of 15
  1. #1
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default trying to send a file name to a class that will read the file

    QuestionSet is a class that has a constructor that requires a filename to be input.
    I tried to create a new QuestionSet object in the main program as follows:

    QuestionSet questions = new QuestionSet(questionFileName);

    when compiled, I get an error message that seems to indicate I need a throws IO Exception statement,
    so I tried the following:

    QuestionSet questions = new QuestionSet(questionFileName) throws FileNotFoundException;

    when I compile this it says I need another ;

    Any ideas -- this is my first attempt to try and read from a file. I know the file exists and I know that the program can find it.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: trying to send a file name to a class that will read the file

    Have you read the Java tutorial on Exceptions? If it were me, that would be the first place I'd look for a solution.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: trying to send a file name to a class that will read the file

    Quote Originally Posted by Jadeclan View Post
    I know that the program can find it.
    Really? What evidence do you have to support that?

  4. #4
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: trying to send a file name to a class that will read the file

    I am taking a course to learn Java. The current assignment deals with using methods of user created classes. We have not been taught IOExceptions or anything close, but rather for this assignment have been given some preexisting code we are supposed to add to.

    the code given, allows us to double click on a file. I think I'm supposed to send the filename to a class to have the class process it.

    I've read the tutorial and it's at least 3 weeks over my head.

    is there some simple answer like if IOExcpetion just stop?

  5. #5
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: trying to send a file name to a class that will read the file

    The evidence is that when I compile the program without my attempted add on's I get prompted to choose a file that is in fact there.

  6. #6
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: trying to send a file name to a class that will read the file

    Java Code:
    import java.io.*;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    
    public class MarkingProgram
    {
        public static void main(String[] args) throws FileNotFoundException
        {
            String questionFileName = selectFile("Double-Click to Select Questions File:");
            String answerFileName = selectFile("Double-Click to Select Answers File to Mark:");
            
            // TO DO: ADD YOUR MAIN PROGARM CODE HERE (DELETE THIS COMMENT)
            
            String results = "TO DO: BUILD THE RESULT STRING";
    
            displayResults(results); 
        }
        
        
        
        // The following methods are provided for your convenience.  Do NOT modify them!
        
        // Allows the user to select a file.  Returns the pathname of that file as a String.
    
        public static String selectFile(String prompt)
        {
            JFileChooser fileChooser = new JFileChooser(".\\test data");
    
            fileChooser.setDialogTitle(prompt);
            fileChooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
            fileChooser.setControlButtonsAreShown(false);
            fileChooser.showOpenDialog(null);
    
            return fileChooser.getSelectedFile().getPath();
        }
    
        
        // Displays the "results" String for the user, in a dialog window.
        
        public static void displayResults(String results)
        {
            JOptionPane.showMessageDialog(null, results, "Results:", JOptionPane.PLAIN_MESSAGE);
        }
    }

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: trying to send a file name to a class that will read the file

    The simple answer is to read the Exception tutorial. Just because you haven't covered something in class does not mean you cannot learn it on your own.

  8. #8
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: trying to send a file name to a class that will read the file

    Sigh

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: trying to send a file name to a class that will read the file

    Oh boo hoo! Aren't we mean expecting you to do some research.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: trying to send a file name to a class that will read the file

    You have been told to go read a tutorial. It will have all the information that you need. However you do not want to do that and expect us to take the time to write up an explanation which will basically duplicate what is already in that tutorial that you will not read. Why should we waste our time doing that?

  11. #11
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: trying to send a file name to a class that will read the file

    I read about 20 pages of tutorial that I understood about 1% of. This assignment has nothing to do with the handling of excpetions which will come in next years course.

    Thanks a bunch for the helpful insight on learning

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: trying to send a file name to a class that will read the file

    Quote Originally Posted by Jadeclan View Post
    I read about 20 pages of tutorial that I understood about 1% of.
    So what is that 1% that you understood? What is the 99% that you didn't understand? As I said anything we post will be duplicating what is in the tutorial. If you don't understand that, how do you magically expect to understand the same information posted by us?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: trying to send a file name to a class that will read the file

    Quote Originally Posted by Jadeclan View Post
    I read about 20 pages of tutorial that I understood about 1% of. This assignment has nothing to do with the handling of excpetions which will come in next years course.

    Thanks a bunch for the helpful insight on learning
    We're not going to write your code for you, but will be happy to answer direct questions with direct answers. I agree with Junky -- ask any questions you'd like about confusing bits in the tutorials and we'll be more than happy to try to help as best we can. But just complain about not understanding and there's not a whole lot we can do.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: trying to send a file name to a class that will read the file

    Does seem strange to be given an assignment that by its very nature (at least from your description) requires knowledge of exception handling, to not have actually been taught how exceptions work.

    Are you sure you haven't missed something?

  15. #15
    Jadeclan is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: trying to send a file name to a class that will read the file

    The assignment is a first year U course, that won't cover GUI or even file handling till next year. In any event, I used the old bang at the keyboard until something happens method. As far as I can tell, it has something to do with changing my public main ...... throws FileNotFoundException to throws IOException.

    Anyway, thanks for reading and trying.

    PS if I ask someone to read chinese and I send them to a chinese book to learn, I wouldn't be surprised if they came back with a dumb sounding, looking like they didn't try question. The material was simply over my head. It won't be in a couple months, but it is now.

    sorry for asking a question that appeared stupid

Similar Threads

  1. Class employee,company,test: Read data from text file.
    By jeskoston in forum New To Java
    Replies: 4
    Last Post: 03-01-2011, 01:50 AM
  2. read file and send them to different arraylist
    By e1007 in forum New To Java
    Replies: 8
    Last Post: 12-23-2010, 05:40 PM
  3. How would I read a .class file?
    By drone13 in forum Advanced Java
    Replies: 4
    Last Post: 01-25-2010, 08:42 AM
  4. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  5. Replies: 0
    Last Post: 02-11-2009, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •