Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Why won't my method become defined in Java?

I have the below code and its giving me an error saying that readFile method is not defined. What is the issue?

double[] grades = new double[SIZE];

Stat statistics = new Stat(grades, count);

System.out.print(" Enter file name: ");

fileName = scan.next();

readFile(fileName);

public String readFile(String name)

{

for(int i = 0; i < SIZE; i++)

{

inFile = new Scanner(new File( fileName ));

while(inFile.hasNextDouble())

{

grades[i] = inFile.nextDouble();

i++;

}

return grades[i];

}

inFile.close();

}

System.out.print(count);

Update:

Actually here's the full code:

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Scanner;

public class StatDriver {

public static void main (String[] args)throws IOException

{

Scanner scan = new Scanner (System.in);

final int SIZE = 20;

int count = 0;

Scanner inFile = null;

String fileName;

double[] grades = new double[SIZE];

Stat statistics = new Stat(grades, count);

System.out.print(" Enter file name: ");

fileName = scan.next();

readFile(fileName);

public static String readFile(String fileName)

{

for(int i = 0; i < SIZE; i++)

{

inFile = new Scanner(new File( fileName ));

while(inFile.hasNextDouble())

{

grades[i] = inFile.nextDouble();

i++;

}

return grades[i];

}

inFile.close();

}

System.out.print(count);

}

}

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    I coudn't understand whats the idea behind your code. However, I have modified the code to run without errors. Here it is

    import java.io.File;

    import java.io.FileNotFoundException;

    import java.io.IOException;

    import java.util.Scanner;

    public class StatDriver {

    public static void main (String[] args)throws IOException

    {

    Scanner scan = new Scanner (System.in);

    final int SIZE = 20;

    int count = 0;

    String fileName;

    StatDriver statistics = new StatDriver();

    System.out.print(" Enter file name: ");

    fileName = scan.next();

    double[] grades = new double[SIZE];

    grades= statistics.readFile(fileName);

    System.out.print(grades);

    }

    public double[] readFile(String fileName)throws FileNotFoundException

    {

    final int SIZE = 20;

    double[] grades = new double[SIZE];

    for(int i = 0; i < SIZE; i++)

    {

    Scanner inFile = new Scanner(new File( fileName ));

    while(inFile.hasNextDouble())

    {

    grades[i] = inFile.nextDouble();

    i++;

    }

    }

    return grades;

    }

    }

  • 1 decade ago

    I don't see a closing } for your main() method.

    +add

    I put this into my NetBeans and also see that your readFile method is supposed to return a String.

    inFile is defined in main and hence is not available in readFile. You probably should make inFile a class level variable or change the statement in readFile to:

    Scanner inFile = new Scanner(new File(fileName));

  • ?
    Lv 6
    1 decade ago

    a couple of errors

    close the main function braces before the starting of readFile function

    scanner object inFile is local to main

    SIZE is local to main

    grades array is local to main

    readFile should return int array not String

    store the array returned by readFile in an int array

    i think you are trying to nest functions in java that is wrong

Still have questions? Get your answers by asking now.