Answered step by step
Verified Expert Solution
Link Copied!

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: 2,3,4
Enter hole Number: 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at golfgames.GolfGames.findAvgForHole(GolfGames.java:69)
at golfgames.GolfGames.main(GolfGames.java:113)
/Users/masonmcclung/Library/Caches/NetBeans/18/executor-snippets/run.xml:111: The following error occurred while executing this line:
/Users/masonmcclung/Library/Caches/NetBeans/18/executor-snippets/run.xml:68: Java returned: 1 package golfgames;
import java.util.Scanner;
public class GolfGames {
// Method to extract player names from the input string
public static String[] getNames(String inputStringNames){
String names[]= inputStringNames.split(",");
return names;
}
// Method to parse and organize scores from the input string
public static int[][] getScores(String inputScoresString){
String scoresString[]= inputScoresString.split("<>");
int holes = scoresString.length;
int numberOfPlayers = scoresString[0].split(",").length;
int scores[][]= new int[holes][numberOfPlayers];
for (int i =0; i < holes; i++){
int individualScores[]= new int[numberOfPlayers];
String individualScoresString[]= scoresString[i].split(",");
for (int j =0; j < numberOfPlayers; j++){
individualScores[j]= Integer.parseInt(individualScoresString[j]);
}
scores[i]= individualScores;
}
return scores;
}
// Method to find the winner based on total scores
public static String findWinner(String[] names, int[][] scores){
int[] addedScores = new int[names.length];
for (int[] score : scores){
for (int j =0; j < scores[0].length; j++){
addedScores[j]+= score[j];
}
}
int minimumTotalIndex =0;
for (int i =0; i < addedScores.length; i++){
if (addedScores[i]< addedScores[minimumTotalIndex]){
minimumTotalIndex = i;
}
}
return names[minimumTotalIndex];
}
// Method to calculate the average for a specific hole
/**
*
* @param scores
* @param holeNum
* @return
*/
public static double findAvgForHole(int[][] scores, int holeNum){
double total =0;
for (int i =0; i < scores[0].length; i++){
total += scores[holeNum -1][i];
}
return total / scores[0].length;
}
// Method to find players below the average for a specific hole
public static String[] searchPlayersBelowAvg(String names[], int[][] scores, int holeNum, double avgForHole){
int belowAvgPlayers =0;
for (int i =0; i < names.length; i++){
if (scores[holeNum -1][i]< avgForHole){
belowAvgPlayers++;
}
}
String[] result = new String[belowAvgPlayers];
int index =0;
for (int i =0; i < names.length; i++){
if (scores[holeNum -1][i]< avgForHole){
result[index++]= names[i];
}
}
return result;
}
// Main method for user interaction
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
// User input for names and scores
System.out.print("Enter names: ");
String inputStringNames = sc.nextLine();
System.out.print( "Enter scores: ");
String inputScoresString = sc.nextLine();
String names[]= getNames(inputStringNames);
int scores[][]= getScores(inputScoresString);
String winner = findWinner(names, scores);
// User input for hole number
System.out.print("Enter hole Number: ");
int holeNumber = sc.nextInt();
// Calculations and output
double avgForHole = findAvgForHole(scores, holeNumber);
String[] playersBelowAvg = searchPlayersBelowAvg(names, scores, holeNumber, avgForHole);
System.out.println("The winner is: "+ winner);
System.out.println("The average for Hole "+ holeNumber +" is: "+ avgForHole);
System.out.print("The players below the average for Hole "+ holeNumber +" are: ");
for (String playersBelowAvg1 : playersBelowAvg){
System.out.print(playersBelowAvg1+"");
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

LO2 Describe the various purposes of performance appraisals.

Answered: 1 week ago