jeudi 26 janvier 2017

Football Scores Program Not Catching Errors

I'm relatively new to Java with no previous programming experience. I'm writing a Java program to capture football scores from user input using the Scanner class. It's supposed to capture up to 100 inputs or until the user either types "stop" for the program to stop and echo back the input or "totals" for the program to then display all of the totals. I have it doing most of this now but the problem I'm having is it needs to be able to detect missing scores, team names, or when the user tries to input incorrectly such as inputting a String value into an int or vice versa.

Below is my code

Any help would be highly appreciated!

import static java.lang.System.out;
import java.util.Scanner;
public class resultsGenerator {
public static void main (String[] args) {

          //All variables and arrays
         String[] home_team_name = new String[100];
         String[] away_team_name = new String[100];
         int[] home_team_score = new int[100];
         int[] away_team_score = new int[100];
         int loopCount = 0;                                       
         int invalidEntry = 0;                    
         int totalMatches = 0;           
         String input;
         String split = ":";
         String[] splitter = new String[4];
         int homeScoreTotal = 0;         
         int awayScoreTotal = 0;
         int homeScoreMax = home_team_score[3];              
         int awayScoreMax = away_team_score[4];               

         // Scanner for user input
         Scanner userinput = new Scanner (System.in);

         // Instructions
         out.println("Enter the football scores in this format:\n"
                      + "'Home Team : Away Team : Home Score : Away Score'\n"
                      + "Input 'stop' to stop the program or 'totals' to display totals.");

         // For loop to repeat 100 times and count how many times its looped
         for (loopCount = 0; loopCount <= 100 ; loopCount++) {

         // Data entered here
               input = userinput.nextLine();   

         // Removes the spaces between the colons
               input = input.replace(" : ", ":");                  

         // User enters 'stop' and the loop ends
         if (input.equalsIgnoreCase("stop")){
             out.println("User has stopped the program.");
         break;                                      
         }

         // if user enters Totals the following statistics are shown and returns back to user input
         else if (input.equalsIgnoreCase("totals")){
               out.println("Totals");
               out.println("-------------------------------------");
               out.println("Total Number of Matches Played: " + totalMatches );
               out.println("Total Home Scores: " + homeScoreTotal );
               out.println("Total Away Scores: " + awayScoreTotal );
               out.println("Highest Home Score: " + homeScoreMax );
               out.println("Highest Away Score: " + awayScoreMax );
               out.println("Total Number of Invalid Entries: " + invalidEntry );
               out.println("-------------------------------------");
               out.println("The program will now stop.");
               loopCount--;
         break;
         }

         try {
         // String split into 4 different variables
         splitter = input.split(split);

         // String is split into 4 different variables and stored in array
         home_team_name [loopCount] = splitter[0];
         away_team_name [loopCount] = splitter[1];
         home_team_score [loopCount] = Integer.parseInt(splitter[2]);
         away_team_score [loopCount] = Integer.parseInt(splitter[3]);
         totalMatches++;

         // variables used for finding the total sum for home and away scores
         homeScoreTotal += home_team_score[loopCount];
         awayScoreTotal += away_team_score[loopCount];

         // loops used for finding the highest score in each team 
         for (int currentScore = 0; currentScore < home_team_score.length; currentScore++ ) {
                if (home_team_score[currentScore] > homeScoreMax) {
                      homeScoreMax = home_team_score[currentScore];
                }
         }

         for (int currentScore = 0; currentScore < home_team_score.length; currentScore++ ) {
                if (away_team_score[currentScore] > awayScoreMax) {
                      awayScoreMax = away_team_score[currentScore];
                }
         }
   }


         //Error catch block for home team score or away team score
         //If input is invalid adds to invalid loop count and displays in totals

         catch (java.lang.NumberFormatException invalidTeam){
               out.println("Please enter a valid home team score or away team score.");
               invalidEntry++;
         }

         //Error catch block for home team name or away team name
         //If input is invalid adds to invalid loop count and displays in totals

         catch (java.lang.ArrayIndexOutOfBoundsException invalidScore){
             out.println("Please enter a valid home team name or away team name.");
             invalidEntry++;
         }
         }

         //Counts how many times the loopCount has executed and displays all results
         for (; loopCount > 0; loopCount--)
         {
               out.println(home_team_name[loopCount-1] + " "
                        + "[" + home_team_score[loopCount-1] + "]" 
                        + " | " + away_team_name[loopCount-1] 
                        + " [" + away_team_score[loopCount-1] + "] ");
         }


         //Closes the scanner
         userinput.close();

} }

Aucun commentaire:

Enregistrer un commentaire