Results 1 to 6 of 6
- 01-15-2009, 04:47 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
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) %
- 01-15-2009, 04:55 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
you need to try catch blockJava Code:Scanner(File source) throws FileNotFoundException
- 01-15-2009, 05:04 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
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
- 01-15-2009, 05:16 AM #4
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
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?? =[
- 01-15-2009, 05:17 AM #5
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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 pathLast edited by mtyoung; 01-15-2009 at 05:28 AM.
- 01-15-2009, 06:07 AM #6
Similar Threads
-
passing a parameter
By aarthi2learn in forum AWT / SwingReplies: 4Last Post: 12-22-2008, 05:46 AM -
Can a method take itself as parameter?
By bukake in forum New To JavaReplies: 10Last Post: 09-06-2008, 09:26 PM -
How to pass mysql string through a url parameter to a detail page query
By kwesiaryee in forum New To JavaReplies: 1Last Post: 08-22-2008, 06:28 PM -
How to pass two parameter in struts-config using dynaActionForm
By 82rathi.angara in forum Web FrameworksReplies: 2Last Post: 06-10-2008, 02:43 PM -
arugment/parameter
By ravian in forum New To JavaReplies: 5Last Post: 01-04-2008, 09:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks