FileWriter проверяет файл и возвращает указанный c элемент данных по критериям - PullRequest
0 голосов
/ 08 мая 2020

Я должен написать программу с компонентом FileWriter, который проверяет все строки текстового файла и может возвращать наибольший процент в этом файле. И я также должен принимать ввод для «игрока» и возвращать его элементы данных. Нам не разрешено хранить списки массивов. Я застрял, потому что кажется, что я разрешаю программе распознавать и сохранять нужного человека, но я не вижу этого в файле результатов. Я свяжу всю свою программу, так как это беспорядок, и я действительно не уверен, что это вызывает проблему.

Пример набора данных и инструкций: enter image description here

Пример выполнения: enter image description here

import java.util.Scanner;
import java.io.*;  //import all the classes in Io package

public class NBADataProgram
{
   public static void main(String [] args) throws IOException
   {
      
      
         //declare variables
      String NAME, TEAM, POS, favPlayer = "";
      double FGM, FGA, pm, FTM, FieldGoalPercent;
      int counter = 0, center = 0, powForward = 0, smForward = 0, shootGuard = 0, pointGuard = 0;
      double highTwoPoint = 0.0;
      String favTeam = "", favPos = "";
      double favPerc = 0;
      
      
        
      File dataFile  = new File("NBA_Stats.txt");
      Scanner inFile = new Scanner( dataFile      );
      
      FileWriter fw = new FileWriter("NBA_Result.txt", false); //true - append, false - overwrite
      PrintWriter outFile = new PrintWriter( fw );
      
      
      
      inFile.nextLine(); //read the first line and discard it
      
      Scanner keyboard = new Scanner(System.in);
      System.out.print("\nWho is your favorite player? ");
      favPlayer = keyboard.next();
      
      while( inFile.hasNext() ) {   //As long as the file has data item
      
         NAME = inFile.next   ();
         TEAM = inFile.next   ();
         POS  = inFile.next   ();
         FGM  = inFile.nextDouble();
         FGA  = inFile.nextDouble();
         pm   = inFile.nextDouble();
         FTM  = inFile.nextDouble();
         
         
         counter++;
         if(POS.equals("C")){
            center++;
         }
         else{
            if(POS.equals("PF")){
               powForward++;
            }
            else{
               if(POS.equals("SF")){
                  smForward++;
               }
               else{
                  if(POS.equals("SG")){
                     shootGuard++;
                  }
                  else{
                     pointGuard++;
                  }
               }
            }
         }
         
         
         
         FieldGoalPercent = (FGM/FGA) * 100;
         
         NAME = NAME.replace("_", " ");
         
         if( FieldGoalPercent > highTwoPoint){
            highTwoPoint = FieldGoalPercent;
         }
            
         if( favPlayer.equalsIgnoreCase(NAME)){
            favTeam = TEAM;
            favPos = POS;
            favPerc = FieldGoalPercent;
         }
         
         
            //save to output file
         outFile.printf( "%-25s %-8s %-8s", 
            NAME, TEAM, POS);   
            
         outFile.printf( "%-8.2f %-8.2f %-8.2f %-8.2f", 
            FGM, FGA, pm, FTM);
          
         outFile.printf( "%-5.2f%% %n", FieldGoalPercent);
      }   
      
      
         
      outFile.printf("%nTotal Number of Players: %-2s",counter);
      outFile.printf("%n     Center: %-2s",center);
      outFile.printf("%n     Power Forward: %-2s",powForward);
      outFile.printf("%n     Small Forward: %-2s",smForward);
      outFile.printf("%n     Shooting Guard: %-2s",shootGuard);
      outFile.printf("%n     Point Guard: %-2s",pointGuard);
      outFile.printf("%nHighest 2-Point Field Percentage: %-4.2f%%",highTwoPoint);
      
      outFile.printf("%n%nMy Favorite Player: %-3s  %-3s  %-3s", favTeam, favPos, favPerc);
      
      
         //close
      inFile.close();  //optional
      outFile.close(); //required
      
      System.out.println("Done, check the output file.");
      System.out.println(counter);
      System.out.println(favTeam);
      
      System.out.printf("%nMy Favorite Player: %-4s %-2s %-4.2f%%", favTeam, favPos, favPerc);
      
   }//end main  
} //end class
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...