Java Beginners

Moderators: zibadian
Number of threads: 1233
Number of posts: 2675

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
File Checking Error, any ideas? Posted by jakedebus on 4 Oct 2012 at 2:13 PM
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( "\n 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:"
+ "\n\t Grades.txt"
+ "\n\t Grades2.txt"
+ "\n\t Grades3.txt"
+ "\n\t Grades4.txt"
+ "\n\t 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("\n 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:"
+ "\n\t Total Number of Kids: " + kids
+ "\n\t Average of All Grades: " + average
+ "\n\t Highest Grade: " + max
+ "\n\t Lowest Grade: " + min
+ "\n\t Number of Students who Passed: " + passing
+ "\n\t Number of Students who got an A: " + a
+ "\n\t Number of Students who got a B: " + b
+ "\n\t Number of Students who got a C: " + c
+ "\n\t Number of Students who got a D: " + d
+ "\n\t Number of Students who got an F: " + f );

}
}










 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.