未读取第一个和最后一个数据项.(爪哇)


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 ("tt***COURSE  AVERAGE  REPORT***");
       outFile.println ("");
       outFile.println ("");
       outFile.println ("NAMEttttCOURSE AVERAGEt");
       outFile.println ("");
       //--------------------------------------------------------------
       outFile2.println ("ttt***C.I.T. MAJOR REPORT***");
       outFile2.println ("");
       outFile2.println ("");
       outFile2.println ("NAMEttttFINAL SCOREttDIFFERENTIALt");
       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 + "tt" + grade); count++;
         //----------------------------------------------------------------------------
         if(major=="CIT"){
         outFile2.println (firstName + lastName + "tt" + finalExam + "tt" + differential); count++;
         }
      }
    }
         finally
          {
               System.out.println ("n---PROCESSING COMPLETED---nn");
               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(); }
          }
   }
}

输入文件文本:

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\文件>处理\文件处理.java [行: 21] 警告:永远不会读取局部变量 stuID 文件: C:\用户\x\文档\课业\CIT 249 JAVA II\文件>处理\文件处理.java [行:29] 警告:永远不会读取局部变量 gpa。

运行时出错:

java.util.InputMismatchException at java.util.Scanner.throwFor(未知来源) at java.util.Scanner.next(未知来源) at java.util.Scanner.nextDouble(未知来源) at FileProcessing.main(FileProcessing.java:93) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(未知来源) at>edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

我看到的第一个问题是你没有在每次nextInt()之后和nextDouble()之后调用nextLine()。这样做很重要的原因是nextXXX()不会使用换行符。因此,下次您获取令牌时,您将抓住换行符。这就是您在获取major令牌后出现输入不匹配的原因。 gpa永远不会被阅读,因为你的程序从来没有真正走得那么远。 Scanner的行为是,当它被输入一个坏令牌时,它会跳过它。

不要使用hasNext() .请改用 hasNextLine() 以避免跳过标记和不使用换行符。有关更多详细信息,请参阅此 StackOverflow 问题:使用 Scanner 获取用户输入。阅读接受的答案。它将解释为什么您跳过第一个令牌。

查看 API for Scanner : http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html 可能会对您有所帮助。我建议至少阅读"本地化数字"部分,尽管整个内容很好读。

相关内容

  • 没有找到相关文章

最新更新