samedi 13 juin 2020

Having trouble with running file program

enter image description here I have a class assignment that I could use some help with. I have a file on my computer called students which contains the first name, last name, and score of made up students. I have to display the file but with the scores in ascending order and with the average but I keep receiving the following error,

 Exception in thread "main" java.lang.NumberFormatException: For input string: "e   Sco"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:658)
    at java.base/java.lang.Integer.parseInt(Integer.java:776)
    at names.Names.main(Names.java:47)

I contacted my teacher for help and he said my file is probably not spaced out properly when I split the line of code, but I followed the example he showed and he takes a long time to answer and I need to finish this assignment pretty soon. I'm also new to java so any help would be appreciated and thank you in advance :)

          import java.io.*;
               public class Names {

                 /**
                  * @param args the command line arguments
                  */
                     public static void main(String[] args) {
                       File textFile = new File(""); //insert file location
                       FileReader in;
                       BufferedReader readFile;
                       //setting up arrays with max 25 values
                       String[] FIRSTNAME = new String[25];
                       String[] LASTNAME = new String[25];
                       int[] SCORE = new int[25];

                      //temporary field variables to split line of data
                      String tmpFIRST, tmpLAST, tmpSCORE;
                      int location;
                      String lineOfText;

                       //counts number of records in File
                       int c = 0;

                       try{
                          in = new FileReader("C:\\Users\\Vic_g\\OneDrive\\Desktop\\students.txt");
                          readFile = new BufferedReader(in);
                        while((lineOfText = readFile.readLine())!=null){
                           c+=1;
                        //split line record fields into temporary variables
                          tmpFIRST = lineOfText.substring(0,5);
                          tmpLAST = lineOfText.substring(5,20);
                          tmpSCORE = lineOfText.substring(20,27);
                          FIRSTNAME[c] = tmpFIRST;
                          LASTNAME[c] = tmpLAST;
                          SCORE[c] = Integer.parseInt(tmpSCORE);
                       }

                      //call method to display File
                        DISP_Array(FIRSTNAME,LASTNAME,SCORE,c);
                        readFile.close();
                        in.close();
                        }catch(FileNotFoundException e){
                        System.out.println("cannot find file");
                       }catch(IOException e){
                         System.out.println("Problem reading file");
                       }                
                      }

            public static void DISP_Array(String[] firstname, String[] lastname, int[] score, int c){
                 //average is scores /counter
                 int average;
                 String tmpFIRST, tmpLAST;
                 int tmpSCORE;
                 //display headings
                 System.out.println("FIRST NAME     LASTNAME     SCORE");
                 System.out.println("-----------------------------------");
                //display in ascending order by score
                 for(int j=0; j<score.length-1; j++){
                    for(int k=j+1; k <score.length; k++){
                     if(score[k] < score[j]){
                     //switch scores
                      tmpSCORE = score[k];
                      score[k] = score[j];
                      score[j] = tmpSCORE;
                     //switch first names
                      tmpFIRST = firstname[k];
                      firstname[k] = firstname[j];
                      firstname[j] = tmpFIRST;
                     //switch last names
                     tmpLAST = lastname[k];
                     lastname[k] = lastname[j];
                     lastname[j] = tmpLAST;

                 //for loop for average
                    for(int i=1; i<=c; i++){
                     average = score[i] / c;
                     System.out.println(firstname[i] + " " + lastname[i] + " " + score[i]);
                      System.out.println("average = " + average);
                 }
              }
            }
           }
           }

          }

Aucun commentaire:

Enregistrer un commentaire