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?

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.

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()];

ClockShop.java:9: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
private String[] clocks = new String[index()];

have you tried to catch it yet?
as in:

private String[] clocks;
try{
  clocks = new String[index()];
}
catch(FileNotFoundException e){
// handleTheException
}

i checked everywhere i could for the answer.

Really ?? Then you would found the answer already, making such claims basically will just irritate some people (like me) trying to help you. And it also illustrates that you did not care to even glance through the article I had linked to on how to ask questions the smart way.

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.

What stultuske has done in is catch the FileNotFoundException which was the first option I had suggested.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.