Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am receiving the following error when running the java code below, please advise: run: Enter names: Mason, Jerry, Tommy Enter scores: 2 , 3
I am receiving the following error when running the java code below, please advise: run:
Enter names: Mason, Jerry, Tommy
Enter scores:
Enter hole Number:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index out of bounds for length
at golfgames.GolfGames.findAvgForHoleGolfGamesjava:
at golfgames.GolfGames.mainGolfGamesjava:
UsersmasonmcclungLibraryCachesNetBeansexecutorsnippetsrunxml:: The following error occurred while executing this line:
UsersmasonmcclungLibraryCachesNetBeansexecutorsnippetsrunxml:: Java returned: package golfgames;
import java.util.Scanner;
public class GolfGames
Method to extract player names from the input string
public static String getNamesString inputStringNames
String names inputStringNames.split;
return names;
Method to parse and organize scores from the input string
public static int getScoresString inputScoresString
String scoresString inputScoresString.split;
int holes scoresString.length;
int numberOfPlayers scoresStringsplitlength;
int scores new intholesnumberOfPlayers;
for int i ; i holes; i
int individualScores new intnumberOfPlayers;
String individualScoresString scoresStringisplit;
for int j ; j numberOfPlayers; j
individualScoresj Integer.parseIntindividualScoresStringj;
scoresi individualScores;
return scores;
Method to find the winner based on total scores
public static String findWinnerString names, int scores
int addedScores new intnameslength;
for int score : scores
for int j ; j scoreslength; j
addedScoresj scorej;
int minimumTotalIndex ;
for int i ; i addedScores.length; i
if addedScoresi addedScoresminimumTotalIndex
minimumTotalIndex i;
return namesminimumTotalIndex;
Method to calculate the average for a specific hole
@param scores
@param holeNum
@return
public static double findAvgForHoleint scores, int holeNum
double total ;
for int i ; i scoreslength; i
total scoresholeNum i;
return total scoreslength;
Method to find players below the average for a specific hole
public static String searchPlayersBelowAvgString names int scores, int holeNum, double avgForHole
int belowAvgPlayers ;
for int i ; i names.length; i
if scoresholeNum i avgForHole
belowAvgPlayers;
String result new StringbelowAvgPlayers;
int index ;
for int i ; i names.length; i
if scoresholeNum i avgForHole
resultindex namesi;
return result;
Main method for user interaction
public static void mainString args
Scanner sc new ScannerSystemin;
User input for names and scores
System.out.printEnter names: ;
String inputStringNames scnextLine;
System.out.print "Enter scores: ;
String inputScoresString scnextLine;
String names getNamesinputStringNames;
int scores getScoresinputScoresString;
String winner findWinnernames scores;
User input for hole number
System.out.printEnter hole Number: ;
int holeNumber scnextInt;
Calculations and output
double avgForHole findAvgForHolescores holeNumber;
String playersBelowAvg searchPlayersBelowAvgnames scores, holeNumber, avgForHole;
System.out.printlnThe winner is: winner;
System.out.printlnThe average for Hole holeNumber is: avgForHole;
System.out.printThe players below the average for Hole holeNumber are: ;
for String playersBelowAvg : playersBelowAvg
System.out.printplayersBelowAvg;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started