| | |
array index and returning
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Solved Threads: 0
so heres the issue im having. i have this class that im suppose to make for a tester class. i have to declare an array of string at the class level. there is method call to read the context of a file and put into the array. the problem im having is how do i get the number of elements the array needs? i cant change the tester class. iv tried calling a method ie private String[] clocks = new String[index()]; but its saying that the filenotfoundexception needs to be caught or thrown. i threw the exception. any ideas?
Last edited by Zelores; Dec 8th, 2008 at 2:45 am.
honestly I can't make any sense of what you are trying to say. You are mentioning that you want to do some String manipulation but then suddenly you are talking about the compiler complaining about the FileNotFoundException being not caught or mentioned in the throws clause.
Please paste your complete code and the actual compile errors that you are getting and what is the exact motive of your program. Also while you are at it, you could read this article on how you should be actually asking questions on a public forum.
Now from what I see in your post I am assuming that some sort of file manipulation operation is being performed in the index() method, and throws a FileNotFoundException in case some file access error occurs, so either you handle the exception by catching it or at least add it to the "throws" clause of the method (and methods where this method is being used if you are not catching it there either and so on) where you have written this code.
Please paste your complete code and the actual compile errors that you are getting and what is the exact motive of your program. Also while you are at it, you could read this article on how you should be actually asking questions on a public forum.
Now from what I see in your post I am assuming that some sort of file manipulation operation is being performed in the index() method, and throws a FileNotFoundException in case some file access error occurs, so either you handle the exception by catching it or at least add it to the "throws" clause of the method (and methods where this method is being used if you are not catching it there either and so on) where you have written this code.
Last edited by stephen84s; Dec 8th, 2008 at 3:32 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
•
•
Join Date: Mar 2008
Posts: 11
Reputation:
Solved Threads: 0
i checked everywhere i could for the answer. this is basically the problem i have. i declared a array at the class level. i have a text file with x lines of strings. how do i find the number of elements i need for the array
import java.io.*;
import java.util.Scanner;
public class ClockShop
{
private String[] clocks = new String[index()];
private int index() throws FileNotFoundException
{
Scanner file = new Scanner(new File("clocks.txt"));
int i = 0;
while(file.hasNextLine())
{
i++;
file.nextLine();
}
file.close();
return i;
}
}
this is the error im getting:
ClockShop.java:9: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
private String[] clocks = new String[index()];
import java.io.*;
import java.util.Scanner;
public class ClockShop
{
private String[] clocks = new String[index()];
private int index() throws FileNotFoundException
{
Scanner file = new Scanner(new File("clocks.txt"));
int i = 0;
while(file.hasNextLine())
{
i++;
file.nextLine();
}
file.close();
return i;
}
}
this is the error im getting:
ClockShop.java:9: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
private String[] clocks = new String[index()];
•
•
•
•
ClockShop.java:9: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
private String[] clocks = new String[index()];
as in:
Java Syntax (Toggle Plain Text)
private String[] clocks; try{ clocks = new String[index()]; } catch(FileNotFoundException e){ // handleTheException }
Last edited by stultuske; Dec 8th, 2008 at 4:47 am. Reason: adding right place
•
•
•
•
i checked everywhere i could for the answer.
Well that aside, most probably you have seen the answer but you could not figure out whether it was the answer. And just as I had mentioned in my first post :-
•
•
•
•
Now from what I see in your post I am assuming that some sort of file manipulation operation is being performed in the index() method, and throws a FileNotFoundException in case some file access error occurs, so either you handle the exception by catching it or at least add it to the "throws" clause of the method (and methods where this method is being used if you are not catching it there either and so on) where you have written this code.
Also you need to learn about Exception Handling in Java, else you will be telling everyone that you finished looking everywhere but could not find a solution everytime you encounter a problem with exceptions.
Last edited by stephen84s; Dec 8th, 2008 at 5:09 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
![]() |
Similar Threads
- Safe Array (C++)
- Help with smallest index of an Array!! (C++)
- Using recursion to search through an array (C++)
- Searches only returning results for certain strings (PHP)
- three dimensional arrays (PHP)
- Help with array stack vs pointer stack (Computer Science)
- Exact meaning of functions returning reference (C++)
- Forms Not Returning Correct Information (JavaScript / DHTML / AJAX)
- can't proceed from here. (C++)
Other Threads in the Java Forum
- Previous Thread: Reading in a text file -- efficiently
- Next Thread: Strange Java Issue!
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp draw eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






