Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IHAVE THESE CODE AND I WANT TO CHANGE IT TO SCANNER FILE .. PLEASE HELP ME WITH

ID: 3759529 • Letter: I

Question

IHAVE THESE CODE AND I WANT TO CHANGE IT TO SCANNER FILE .. PLEASE HELP ME WITH THAT

public void readFile() {
  try {
   
   // checking that file is exist or not, if file does not exists then terminate code.
   File file = new File("Batting.txt");
   if(!file.exists()){
    System.err.println("File does not exists, please provide correct file name.");
    return ;
   }
   
   FileInputStream fstream = new FileInputStream(file);
   BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
   String strLine;
   // this will read batting.txt file line by line
   int arrayIndex = 0; // it maintain array index
   int arraySize = 0; // it store no of elements in array ( array size)
   while ((strLine = br.readLine()) != null) {
    // setting the array size
    if(arraySize==0){
     // if this is first line from file then set the array size by reading no.
     String data[] = strLine.split(" ");
     arraySize = Integer.parseInt(data[0]);
     playerNames = new String[arraySize];
     avgScore = new double[arraySize];
    } else { // setting the array values
     String data[] = strLine.split(",");
     double score = Double.parseDouble(data[1]);
     playerNames[arrayIndex] = data[0];
     avgScore[arrayIndex] = score;
     arrayIndex++;
    }
    //System.out.println(strLine);
   }
   // once Batting.txt file read is done then closing the file input stream
   br.close();
  } catch(IOException e){
   e.printStackTrace();
  }

}

Explanation / Answer

public void readFile() {
       try {

           // checking that file is exist or not, if file does not exists then
           // terminate code.
           File file = new File("Batting.txt");
           if (!file.exists()) {
               System.err
                       .println("File does not exists, please provide correct file name.");
               return;
           }
           Scanner input = new Scanner(file);
           /*FileInputStream fstream = new FileInputStream(file);
           BufferedReader br = new BufferedReader(new InputStreamReader(
                   fstream));*/
           String strLine;
           // this will read batting.txt file line by line
           int arrayIndex = 0; // it maintain array index
           int arraySize = 0; // it store no of elements in array ( array size)
           String playerNames[] = null;
           double avgScore[] = null;
           while (input.hasNextLine()) {
   strLine = input.nextLine();
               // setting the array size
               if (arraySize == 0) {
                   // if this is first line from file then set the array size
                   // by reading no.
                   String data[] = strLine.split(" ");
                   arraySize = Integer.parseInt(data[0]);
                   playerNames = new String[arraySize];
                   avgScore = new double[arraySize];
               } else { // setting the array values
                   String data[] = strLine.split(",");
                   double score = Double.parseDouble(data[1]);
                   playerNames[arrayIndex] = data[0];
                   avgScore[arrayIndex] = score;
                   arrayIndex++;
               }
               System.out.println(strLine);
           }
           // once Batting.txt file read is done then closing the file input
           // stream
           //br.close();
           input.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }