954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using a static method to read data from text file

I am need a little guidance here as to how I am to go about using a static method to read data from a text file. I know how to use a static method to write data to a text file, now I am wondering how to use a static method to read data from a text file. The following code is the code that read the data from the text file in the main method. Now that the code works in the main method, I am wondering how I can create a static method that does this. Thanks in advance to the person that has some ideas for me on this...

int index = 0;
        double[] surfaceGravity = new double[10];
        File gravity = new File("surfaceGravityData.txt");
        Scanner inFile = new Scanner(gravity);
        
        while (inFile.hasNextDouble())
           {
                surfaceGravity[index] = inFile.nextDouble();
                index++;
           }
jdbarry
Newbie Poster
21 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

I'm not totally sure where you're stuck. Have you tried to google this? I did and the first result has an example of a static read . . .

are you unsure of the concepts? are you trying but getting an error? I'm sorry, I'm just not sure what it is you are asking

you can look here - first result returned might be helpful:

http://lmgtfy.com/?q=java+Reading+and+writing+text+files

rapture
Posting Whiz in Training
294 posts since Jul 2007
Reputation Points: 155
Solved Threads: 41
 

Im a little curious, you want to create that method in the same class or in other class an them invoke it??.

danielernesto
Newbie Poster
16 posts since Nov 2007
Reputation Points: 9
Solved Threads: 2
 

The code is the same in a static method as it would be in main. and main is a static method - just look at the method header.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You