Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

File Checking Error, any ideas?

I am having troubles checking if the file is actually a file, then if not it's supposed to run through a loop asking for the user to re enter the filename, it says cannot find method exists()

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.*;

public class Grades
{
public static void main ( String [] args ) throws IOException
{
int grade;
int kids = 0;
int max = 0;
int min = 100;
int passing = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int f = 0;
int sum = 0;
int number;

String input2 = new String("input2");
Scanner input = new Scanner ( System.in );

System.out.println( "
This program is designed to open a .txt file containing grades and display several statistics about the grades.");
System.out.println(" " );
System.out.println( "The availible input files, where you may input grades are:"
+ "
Grades.txt"
+ "
Grades2.txt"
+ "
Grades3.txt"
+ "
Grades4.txt"
+ "
Grades5.txt");
System.out.print( "Please enter the filename that contains the grades you wish to use from the list above: ");
String file = input.nextLine();

File inputFile = new File(file);

while ( file.exists() == false )
{
System.out.println("
File not found");
System.out.print("Please re-enter filename: ");
String file = input.nextLine();
}

Scanner con = new Scanner( inputFile );

while ( con.hasNext( ) )
{
number = con.nextInt( );
kids++;
sum = sum + number;
min = (number < min) ? number : min;

max = (number > max) ? number : max;

if ( number >= 60 )
passing++;

if ( number >= 90 )
a++;
else if ( (number < 90) && (number >= 80) )
b++;

else if ( (number < 80) && (number >= 70) )
c++;

else if ( (number < 70) && (number >= 60) )
d++;

else if ( number < 60 )
f++;
}

double average = sum / kids;

System.out.println( "The class information is as listed below:"
+ "
Total Number of Kids: " + kids
+ "
Average of All Grades: " + average
+ "
Highest Grade: " + max
+ "
Lowest Grade: " + min
+ "
Number of Students who Passed: " + passing
+ "
Number of Students who got an A: " + a
+ "
Number of Students who got a B: " + b
+ "
Number of Students who got a C: " + c
+ "
Number of Students who got a D: " + d
+ "
Number of Students who got an F: " + f );

}
}







Sign In or Register to comment.