2014-04-01 90 views
0
import java.io.*; 
import java.util.Scanner; 

public class FileProcessing 
{ 
    public static void main(String[] args) throws IOException 
    { 
    FileReader inFile = null; 
    PrintWriter outFile = null; 
    PrintWriter outFile2 = null; 
    Scanner inToken = null; 

    String stuID = null; 
    String lastName = null; 
    String firstName = null; 
    int quiz1 = 0, quiz2 = 0, quiz3 = 0; 
    int finalExam = 0; 
    int hw1 = 0, hw2 = 0, hw3 = 0, hw4 = 0, hw5 = 0; 
    int hw6 = 0, hw7 = 0, hw8 = 0, hw9 = 0, hw10 = 0, hw11 = 0; 
    String major = null; 
    double gpa = 0; 

    double differential = 0; 
    double grade = 0; 
    int min = 0; 

    int count = 0; 
    int countA = 0; 
    int countB = 0; 
    int countC = 0; 
    int countD = 0; 
    int countE = 0; 

    inFile = new FileReader("Students.csv"); 
    outFile = new PrintWriter(new FileWriter("CourseReport.txt")); 
    outFile2 = new PrintWriter(new FileWriter("MajorReport.txt")); 

    try 
    { 
     inToken = new Scanner(new BufferedReader(inFile)); 
     inToken.useDelimiter(","); 

     System.out.println ("---OUTPUT FILE BEING PRODUCED---\n"); 

     if (inToken.hasNext()) 
     { 

     // Creates headings for the output file. 
     outFile.println ("\t\t***COURSE AVERAGE REPORT***"); 
     outFile.println (""); 
     outFile.println (""); 
     outFile.println ("NAME\t\t\t\tCOURSE AVERAGE\t"); 
     outFile.println (""); 
     //-------------------------------------------------------------- 
     outFile2.println ("\t\t\t***C.I.T. MAJOR REPORT***"); 
     outFile2.println (""); 
     outFile2.println (""); 
     outFile2.println ("NAME\t\t\t\tFINAL SCORE\t\tDIFFERENTIAL\t"); 
     outFile2.println (""); 

     } 

     while (inToken.hasNext()) 
     { 
     // Reads the input file. 
     stuID = inToken.next(); 
     lastName = inToken.next(); 
     firstName = inToken.next(); 
     quiz1 = inToken.nextInt(); 
     quiz2 = inToken.nextInt(); 
     quiz3 = inToken.nextInt(); 
     finalExam = inToken.nextInt(); 
     hw1 = inToken.nextInt(); 
     hw2 = inToken.nextInt(); 
     hw3 = inToken.nextInt(); 
     hw4 = inToken.nextInt(); 
     hw5 = inToken.nextInt(); 
     hw6 = inToken.nextInt(); 
     hw7 = inToken.nextInt(); 
     hw8 = inToken.nextInt(); 
     hw9 = inToken.nextInt(); 
     hw10 = inToken.nextInt(); 
     hw11 = inToken.nextInt(); 
     major = inToken.next(); 
     gpa = inToken.nextDouble(); 


     //loop to find min homework score. 
      min = hw1; 
     if(hw2 < hw1) 
      min = hw2; 
     if(hw3 < hw2) 
      min = hw3; 
     if(hw4 < hw3) 
      min = hw4; 
     if(hw5 < hw4) 
      min = hw5; 
     if(hw6 < hw5) 
      min = hw6; 
     if(hw7 < hw6) 
      min = hw7; 
     if(hw8 < hw7) 
      min = hw8; 
     if(hw9 < hw8) 
      min = hw9; 
     if(hw10 < hw9) 
      min = hw10; 
     if(hw11 < hw10) 
      min = hw11; 


     // Calculating grades & differential 
     grade = ((quiz1+quiz2+quiz3) * .45) + (finalExam * .3) + 
       (((hw1 + hw2 + hw3 + hw4 + hw5 + hw6 + hw7 + hw8 + hw9 + hw10 + hw11 - min)/10) * .25); 

     differential = grade - finalExam; 

     if(grade < 60) 
      countE++; 
     else if (grade < 70) 
      countD++; 
      else if (grade < 80) 
      countC++; 
      else if (grade < 90) 
      countB++; 
      else countA++; 


     //Output data. 
     outFile.println (firstName + lastName + "\t\t" + grade); count++; 
     //---------------------------------------------------------------------------- 
     if(major=="CIT"){ 
     outFile2.println (firstName + lastName + "\t\t" + finalExam + "\t\t" + differential); count++; 
     } 

     } 
    } 

     finally 
      { 
       System.out.println ("\n---PROCESSING COMPLETED---\n\n"); 

       if (inToken != null) 
       { if (count > 0) 
        { 

     //report totals------------------------------------------------------------------------------- 
        outFile.println (""); 
        outFile.println ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
        outFile.println ("Number of Students that have an A: \t" + countA); 
        outFile.println ("Number of Students that have an B: \t" + countB); 
        outFile.println ("Number of Students that have an C: \t" + countC); 
        outFile.println ("Number of Students that have an D: \t" + countD); 
        outFile.println ("Number of Students that have an E: \t" + countE); 
        outFile.println ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 
     //------------------------------------------------------------------------------------------- 
        outFile2.println (""); 
        outFile2.println ("~~~END~~~"); 

        } 

        else 
        { 
         outFile.println (""); 
         outFile.println ("Input file was empty...no data processed"); 
         outFile.close(); 
     //-------------------------------------------------------------------------------------------- 
         outFile2.println (""); 
         outFile2.println ("Input file was empty...no data processed"); 
         outFile2.close(); 
        } 
        inToken.close(); 
       } 

       if (outFile != null) 
       { outFile.close(); } 

       if (outFile2 != null) 
       { outFile2.close(); } 
      } 
    } 

} 

輸入文件中的文本:第一個和最後一個數據項未被讀取。 (JAVA)

1036,Trick,Larry,66,89,91,57,12,16,10,13,20,20,20,11,13,13,18,BUS,2.4 
1037,Hernandez,Lisa,78,93,57,52,7,10,5,16,13,5,6,18,9,11,18,BIO,3.7 
1038,Isaacs,James,91,85,51,68,6,13,16,19,16,6,10,13,9,6,15,IMD,4.0 
1039,Malta,Allison,68,60,93,72,12,12,11,12,10,11,7,16,9,8,9,BIO,2.6 
1040,Ray,Leah,58,52,74,71,10,19,6,14,8,13,17,5,7,7,10,BIO,2.4 
1041,Mink,Bruce,90,78,63,85,12,7,14,15,11,5,9,5,7,14,18,BUS,2.8 
1042,Minke,Magdalen,100,91,61,73,17,18,15,15,19,18,18,15,15,20,16,BUS,2.9 
1043,Walker,Carol,89,61,65,64,19,16,15,19,15,17,20,16,17,19,18,BUS,2.0 
1044,Young,Christopher,87,85,52,94,15,20,15,18,20,15,18,16,20,18,19,BIO,3.6 
1045,Summers,Charles,86,65,95,92,17,15,16,15,19,19,19,17,19,15,17,BIO,3.9 
1046,Sumner,Sheila,90,71,61,100,15,16,19,16,19,20,15,16,15,19,20,BUS,3.8 
1047,Turner,Marlena,92,91,83,73,20,20,18,19,17,20,17,17,17,15,18,BUS,2.9 
1048,Janes,Scotty,92,61,56,81,20,20,18,16,17,19,17,16,18,17,19,IMD,3.9 
1049,Maddox,Paul,93,96,81,63,18,18,20,16,17,16,20,18,20,15,18,BUS,3.1 
1050,Peal,Ashley,66,97,61,52,18,18,20,17,19,18,19,15,20,15,18,BUS,3.0 
1051,Peel,Sean,69,76,58,66,15,20,15,17,17,19,17,15,18,19,18,BUS,3.9 
1052,Robertson,Brittany,88,95,75,50,19,18,17,20,17,16,15,16,18,18,20,BUS,4.0 
1053,Votaw,Katie,80,64,86,56,18,19,19,20,20,15,18,17,18,17,16,BUS,2.2 
1054,Watson,Luis,37,76,68,90,20,15,18,15,20,20,15,15,20,15,17,BIO,2.5 
1055,Walker,Russell,44,82,54,53,18,19,16,18,15,15,15,17,15,16,19,BIO,2.4 
1056,Walker,Stephanie,97,85,97,89,17,19,20,19,15,19,16,15,18,15,16,BIO,2.1 
1057,Evans,Thomas,78,63,80,56,20,19,20,17,20,15,20,18,17,19,15,BUS,3.8 
1058,Graves,Janssen,91,90,65,61,16,15,20,16,20,16,20,18,18,20,16,BUS,2.3 
1059,Garrett,Edward,68,93,85,64,17,19,18,15,15,18,16,20,17,17,20,IMD,2.9 
1060,Hicks,Shannon,58,62,86,92,15,17,18,20,15,19,15,18,18,18,18,BUS,3.4 
1061,Back,William,90,66,80,53,20,17,19,20,20,19,20,18,18,17,16,BUS,2.5 
1062,Brown,Barbara,100,81,61,58,16,19,15,16,18,15,15,20,17,20,15,BUS,3.6 
1063,Black,Natasha,89,87,60,58,19,18,17,16,17,15,15,16,19,19,19,BIO,2.4 
1064,Franklin,Louis,87,64,82,74,18,16,17,17,20,16,18,17,17,20,16,BUS,2.3 
1065,Lake,Amy,86,55,52,94,15,18,18,17,17,19,18,17,18,19,20,BUS,2.2 
1066,Lawson,Travis,90,55,77,95,17,18,20,20,16,19,18,20,17,16,16,IMD,2.3 
1067,Johnson,David,92,54,57,96,15,17,20,18,17,16,16,16,16,16,18,BUS,2.9 
1068,Jones,Vivian,92,73,82,98,20,15,17,19,16,19,15,19,15,19,16,IMD,2.6 
1069,Johnson,Joshua,93,76,77,83,16,17,18,18,17,20,15,16,16,20,20,IMD,2.5 
1070,Martin,Ashley,66,64,95,89,19,17,18,19,15,17,15,17,15,17,18,BUS,2.9 
1071,Marks,Farrah,89,77,54,65,16,20,16,16,16,15,17,18,15,16,20,BUS,4.0 
1072,Boggs,Ronnie,90,55,73,99,16,18,20,19,15,17,15,20,18,20,15,BIO,3.9 
1073,Biggers,Kara,100,69,92,59,15,15,16,16,18,17,20,17,20,20,17,BUS,3.4 
1074,Nunn,John,0,68,82,63,15,15,19,15,17,20,17,19,16,15,19,BUS,2.8 
1075,Wilson,Diana,45,58,50,100,20,20,20,16,20,16,17,19,19,17,16,BIO,2.0 
1076,Bastin,Molly,55,64,91,81,16,16,16,16,19,16,19,16,16,19,17,BUS,2.2 
1077,Booger,Kayla,88,54,74,99,20,17,15,17,18,19,15,16,18,16,18,BUS,2.3 
1078,Shay,Susan,89,50,78,80,16,20,19,15,18,20,19,16,20,18,15,BUS,3.1 
1079,Shea,Lakesha,82,88,77,79,20,15,19,18,20,19,17,19,19,15,20,BUS,2.7 
1080,Ward,Brian,47,83,51,89,19,17,17,16,20,16,18,19,18,15,15,BUS,2.1 
1081,Smith,Karen,3,60,99,56,15,17,18,17,15,20,18,20,19,18,15,BUS,3.1 
1082,Adams,Jeri,90,52,79,81,15,19,17,19,16,19,17,17,18,18,15,IMD,2.2 
1083,Bengals,Lisabeth,10,73,64,73,20,15,15,17,19,20,16,16,16,19,20,BUS,2.9 
1084,Scott,John,100,55,77,79,15,18,16,16,16,20,17,19,17,19,17,IMD,3.2 
1085,Ball,Margaret,85,79,95,88,16,18,20,16,17,19,19,15,15,19,17,BIO,2.0 
1086,Gaston,Stephen,77,89,86,74,20,16,19,18,19,20,20,18,17,20,17,BUS,2.4 
1087,Newland,Santy,44,82,75,51,15,19,19,19,17,17,18,17,19,17,15,BUS,2.5 
1088,Manning,Stacy,99,51,86,58,17,20,17,16,18,15,20,16,20,15,16,BUS,3.2 
1089,McIntosh,Frances,60,51,61,99,20,18,18,15,18,18,15,15,19,16,16,BIO,2.5 
1090,Jones,Melynn,90,62,71,93,18,18,16,15,16,15,16,18,20,16,15,BUS,2.3 
1091,Kelley,Kamla,90,81,57,81,20,15,18,16,17,19,16,19,17,18,16,BUS,2.6 
1092,Manning,Amy,89,92,57,97,19,18,19,18,20,20,20,16,15,17,15,BUS,3.4 
1093,Ramsey,Kristen,54,96,62,61,15,15,19,15,19,16,15,15,18,18,15,IMD,2.0 

警告編譯時:發現

2警告: 文件:C:\用戶\ X \文檔\功課\ CIT 249 JAVA II \ File> Processing \ FileProcessing.java [line:21] 警告:不會讀取本地變量stuID 文件:C:\ Users \ x \ Documents \ SCHOOLWORK \ CIT 249 JAVA II \ File> Processing \ FileProcessing.java [line:29] 警告:局部變量的GPA從來沒有讀過

錯誤時運行:

java.util.InputMismatchException 在java.util.Scanner.throwFor(來源不明) 在java.util.Scanner.next (來源不明) 在java.util.Scanner.nextDouble(來源不明) 在FileProcessing.main(FileProcessing.java:93) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法) 在sun.reflect.NativeMethodAccessorImpl。在太陽時調用(未知來源) .reflect.DelegatingMethodAccessorImpl.invoke(未知來源) 在java.lang.reflect.Method.invoke(未知來源) 在> edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

回答

1

第一個問題,我看到的是你而不是每個nextInt()nextDouble()後調用nextLine()。這樣做很重要的原因是因爲nextXXX()確實是而不是消耗換行符。所以下次你拿到一個令牌時,你會抓住換行符。這是獲取major令牌後輸入不匹配的原因。 gpa永遠不會被讀取,因爲你的程序從來沒有真正到達那麼遠。 Scanner的行爲是當它被送入一個不好的標記時,它會跳過它。

請勿使用hasNext()。改爲使用hasNextLine()來避免跳過令牌並且不使用換行符。有關更多詳細信息,請參閱此StackOverflow問題:Getting User input with Scanner。閱讀接受的答案。它會解釋你爲什麼跳過你的第一個標記。

您可能需要查看Scanner的API:http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html。我建議閱讀至少直到「本地化數字」部分,儘管整個事情是一個很好的閱讀。

相關問題