Reputation: 11
i got stuck in this assignment. it's about taking some grades from a text file that the user will input than calculating the max , min , mean. the problem is each time i try to compile it it gives me this
St.java:10: readfile(int[],java.lang.String) in St cannot be applied to (int[])
int n = readfile (grades);
Source code:
import java.io.*;
import java.util.Scanner;
public class St
{
public static void main ( String [] a )
{
Scanner kbd = new Scanner(System.in);
int count = 0;
int [] grades = new int [500];
int n = readfile (grades);
System.out.println("What file contains the data?");
String file = kbd.nextLine();
System.out.println("The maximum grade is: " + max(grades,n));
System.out.println("The minimum grade is: " + min(grades,n));
System.out.println("The mean grade is: "+ mean(grades,n));
}
public static double mean(int [] grades,int n)
{
double sum = 0;
for(int i = 0; i < n; i++)
{
sum = sum + grades[i];
}
return sum/n;
}
public static int readfile (int [] grades, String file)
{
int count = 0;
try
{
Scanner f = new Scanner (new File (file)); // name of file
while (f.hasNext()) // checks if there is more input in the file
{
grades[count]=f.nextInt (); // grabs the next piece of input
count++; // moves onto the next piece of input (increasing count)
}
}
catch (IOException e)
{
System.err.println (e);
System.exit(0);
}
return count;
}
public static void writeArray (int [] grades , int n)
{
for (int i = 0; i < n; i++)
{
System.out.println (grades[i]);
}
}
public static int max(int [] y,int m)
{
int mx = 0;
for(int i = 0; i < m; i++)
{
if(y[i] > mx)
{
mx = y[i];
}
}
return mx;
}
public static int min(int [] grades,int n)
{
int mn = 0;
for(int i = 0; i < n; i++)
{
if(grades[i] < mn)
{
mn = grades[i];
}
}
return mn;
}
}
Upvotes: 1
Views: 2053
Reputation: 9913
you are trying to pass
int n = readfile (grades);
but the function you created :
public static int readfile (int [] grades, String file)
is asking for a second variable as a String
you need to change your line :
String fileName= YOUR_FILE_NAME;
int n = readfile (grades,fileName);
Upvotes: 2
Reputation: 53502
You're not using the input file name that you get from user.
Change this:
int n = readfile (grades);
System.out.println("What file contains the data?");
String file = kbd.nextLine();
into
System.out.println("What file contains the data?");
String file = kbd.nextLine();
int n = readfile (grades, file);
So you get the file name from user and pass it on to your method.
Upvotes: 0