java - Why doesn't my program call my methods? -
java methods statithing can't seem while statement on line 56 call methods properly. there i'm doing wrong? new java sort of appreciated! in advance! here code:
import java.util.*; import javax.swing.*; import java.io.*; public class gradecalculator { static string fileinput; static double totalgrade; static int scorecount= 0; static double classaverage; static double score; static double testscore; static double averagescore; static int numberofstudents = 0; static char lettergrade; static string fileoutput; static string nameofstudent; static int numbercount; static int numbercalculatedstudents; static double average = 0; public static void main (string[] args) throws filenotfoundexception { //welcome joptionpane.showmessagedialog(null,"welcome grade calculator! program will\n" + " calculate average percentage of 5 test scores that\n"+ " given in given file once these scores are\n"+ " averaged give student letter grade.","welcome!",joptionpane.plain_message); fileinput = joptionpane.showinputdialog(null, "please enter name of input file wish use program." ,"input file",joptionpane.plain_message); fileoutput = joptionpane.showinputdialog(null, "please enter name of output file wish use program." ,"output file",joptionpane.plain_message); //preparing text files printwriter outfile = new printwriter (fileoutput); file infile = new file (fileinput); scanner reader = new scanner(new filereader(fileinput)); outfile.println("student name test1 test2 test3 test4 test5 average grade"); while(reader.hasnextline()) { nameofstudent = reader.next(); outfile.printf("%n%n%s",nameofstudent); numberofstudents++; score = reader.nextdouble(); calculateaverage(score); calculategrade(averagescore); outfile.printf(" %.2f ", averagescore); outfile.println(" "+lettergrade); } classaverage = classaverage/numbercalculatedstudents; outfile.print("\n\n\n\n\n\n\n\n\n\n\n\nclass average "+ numbercalculatedstudents + "of" + numberofstudents + "is" + classaverage); joptionpane.showmessagedialog(null,"the report has been completed , written file of " + fileoutput +"." +" class average " + classaverage + ". please go output file complete report."); outfile.close(); } public static void calculateaverage(double score) throws filenotfoundexception { scanner reader = new scanner(new filereader(fileinput)); printwriter outfile = new printwriter (fileoutput); while (reader.hasnextdouble() && numbercount <= 5 ) { score = reader.nextdouble(); numbercount++; if (score >= 0 & score <= 100) { outfile.printf("%10.2f",score); scorecount++; average = score + average; } else outfile.printf(" **%.2f",score); } if (average!=0){ numbercalculatedstudents++; average = average/scorecount; averagescore = average; classaverage = average + classaverage; } average = 0; } public static char calculategrade (double averagescore) { if (averagescore >= 90 && averagescore <= 100) lettergrade = 'a'; else if (averagescore < 90 && averagescore >= 80) lettergrade = 'b'; else if (averagescore < 80 && averagescore >= 70) lettergrade = 'c'; else if (averagescore < 70 && averagescore >= 60) lettergrade = 'd'; else if (averagescore < 60 && averagescore >= 0) lettergrade = 'f'; else lettergrade =' '; return lettergrade; } }
without knowing line problem arising on, 2 issues jump out @ me:
at top of while loop:
if (score >= 0 & score <= 100) { outfile.printf("%10.2f",score); scorecount++; average = score + average; } else outfile.printf(" **%.2f",score);}
you have close bracket (}
) after else statement, no open bracket. close bracket looks exiting while loop before want to.
second, looks you're trying return (namely char) in calculategrade method, have specified return type of void on it, means though have return statement, nothing gets returned when call it. don't show you're calling method, can't sure causing problem, suspect. seems want use:
public static char calculateaverage(double score) throws filenotfoundexception{
instead of public static void calculateaverage(double score)...
also, there reason why of these methods static? know making static means?
edit (based on comment):
no. making variable static
makes "class variable", means 1 of them exists objects of class. illustrate:
if have class this:
class test { static int id; }
and run following code:
test t1 = new test(); test t2 = new test(); t1.id = 4; t2.id = 8; system.out.println(t1.id);
it print 8. because, since id static
variable changing on object of class cause change every other object of class.
this opposed "instance variable" 1 of exists every object of class. make id
instance variable, remove static
keyword. if , run same code, print 4, because changing instance variable of t2 has no effect on t1.
make sense?
Comments
Post a Comment