Results 1 to 6 of 6
  1. #1
    daletron3030 is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default I can't seem to pass a file through a parameter

    Hi everyone. Just starting to write up my program right now and i've been stuck for more than an hour on this.

    I'm planning to make a function that has a file as the parameter and I would like to use that function in the main.. but is that not possible or something? I keep getting this error in bold... please help =[

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

    class Students
    {

    //VARIABLES
    protected int count=0;
    protected String []names = new String[40];
    protected int []namespicked = new int[40];

    //CONSTRUCTOR
    public Students()
    {
    }

    //OBJECTS
    public void initialize(File namelist)
    {
    Scanner scanfile = new Scanner(namelist);
    /* for(int a=0; (scanfile.hasNext()); a++)
    {
    names[a] = scanfile.nextLine();
    namespicked[a] = 0;
    count++;
    }
    */ scanfile.close();
    }
    }

    class P1
    {
    public static void main (String [] args) throws Exception
    {

    System.out.println("TESTING");

    }
    }


    [ Wrote 39 lines ]

    intranet (73) % javac randomstudent.java
    randomstudent.java:20: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
    Scanner scanfile = new Scanner(namelist);
    ^
    1 error
    intranet (74) %

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Java Code:
    Scanner(File source) throws FileNotFoundException
    you need to try catch block

  3. #3
    daletron3030 is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    can you please explain that to me and tell me what the code is for that?

    I've also been experimenting.. i even removed that function completely and put

    Scanner scanfile = new Scanner(name.txt);

    in the Main and it's still giving me an error saying

    "cannot find symbol."

    argh this is so frustrating =T

  4. #4
    daletron3030 is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    ok well..... i got this line to work in main

    Scanner scanfile = new Scanner(new File("name.txt"));

    yet if i put the EXACT same line in my function up above in my Students class i get that throw error

    can someone help me out with that?? =[

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Constructor Scanner(File source) throws FileNotFoundException,
    you need to catch the exception

    plz read
    The try Block (The Java™ Tutorials > Essential Classes > Exceptions)
    The catch Blocks (The Java™ Tutorials > Essential Classes > Exceptions)

    in the catch block, you need to handle namelist is not a existence file path
    Last edited by mtyoung; 01-15-2009 at 05:28 AM.

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you need to wrape line20 in a try-catch block.
    OR
    declare the function to throw an exception.

Similar Threads

  1. passing a parameter
    By aarthi2learn in forum AWT / Swing
    Replies: 4
    Last Post: 12-22-2008, 05:46 AM
  2. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 09:26 PM
  3. Replies: 1
    Last Post: 08-22-2008, 06:28 PM
  4. How to pass two parameter in struts-config using dynaActionForm
    By 82rathi.angara in forum Web Frameworks
    Replies: 2
    Last Post: 06-10-2008, 02:43 PM
  5. arugment/parameter
    By ravian in forum New To Java
    Replies: 5
    Last Post: 01-04-2008, 09:43 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
  •