Need to get the index of the largest value in teamScores[] and print the associated string with the matching index from teamNames[]. This is really starting to get on my nerves. I had been able to successfully get the right value for the scores printed but it kept printing the wrong team. When I was trying to troubleshoot I was getting the right team but the wrong score. I am absolutely lost and have no other ideas. Anybody offer some advice? I have to use two separate arrays so I cannot just reduce it to one array. I also have to use a for loop to retrieve the values, so I can't do like I did with the lowScore.
public class SmithJustin_Asgn6 {
public static int highScore(int[] teamScores, int highIndex) {
int max = teamScores[0];
for(int i = 0; i < teamScores.length; i ) {
if(max < teamScores[i]) {
max = teamScores[i];
highIndex = i;
}
}return highIndex;
}
public static int lowScore(int[] teamScores) {
Arrays.sort(teamScores);
int low = teamScores[0];
return low;
}
public static void main(String[] args) {
int highIndex = 0;
Scanner userInput=new Scanner(System.in);
Scanner scoreInput=new Scanner(System.in);
System.out.print("Enter the number of teams would you like to enter data for: ");
int teams=scoreInput.nextInt();
int [] teamScores= new int[teams];
String [] teamNames= new String[teams];
for(int i = 0; i < teams; i ) {
System.out.println("\nTeam " (i) ":");
System.out.println();
System.out.print("Enter Team's name: ");
String teamName=userInput.nextLine();
teamNames[i]=teamName;
System.out.print("Enter Team's score (400-1000): ");
int teamScore=scoreInput.nextInt();
teamScores[i]=teamScore;
System.out.println();
}
highScore(teamScores, highIndex);
lowScore(teamScores);
System.out.println();
System.out.println("The high score is " teamScores[highScore(teamScores, highIndex)] " by team " teamNames[highScore(teamScores, highIndex)] " and the low score is " lowScore(teamScores) ".");
userInput.close();
scoreInput.close();
}
}
Been trying every way to slice it and I am completely stuck
CodePudding user response:
You can create a class to store team name and its score. Then sort the array of class objects based on a comparator. Also, you don't need to use two Scanner
objects.
class Team
{
public int score;
public String name;
}
class SmithJustin_Asgn6 {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
System.out.print("Enter the number of teams would you like to enter data for: ");
int teams = userInput.nextInt();
Team[] teamArray = new Team[teams];
for(int i = 0; i < teams; i ) {
teamArray[i] = new Team();
userInput.nextLine();
System.out.println("\nTeam " (i) ":");
System.out.println();
System.out.print("Enter Team's name: ");
teamArray[i].name = userInput.nextLine();
System.out.print("Enter Team's score (400-1000): ");
teamArray[i].score = userInput.nextInt();
System.out.println();
}
userInput.close();
Arrays.sort(teamArray, new Comparator<Team>() {
@Override
public int compare(Team o1, Team o2) {
return Integer.compare(o1.score, o2.score);
}
});
System.out.println();
System.out.println("The high score is " teamArray[teams - 1].score " by team " teamArray[teams - 1].name " and the low score is " teamArray[0].score ".");
}
}
CodePudding user response:
As mentioned by @Andrey your Arrays.sort
is the main culprit. You need a logic to get the low score index the same as you have done for high score index.
public static int lowScore(int[] teamScores, int lowIndex) {
// Arrays.sort(teamScores);
int low = teamScores[0];
//logic to low score's index
return lowIndex;
}
After you have both the indexes, you can easily get values from respective arrays using them.
In your main
method you are calling the same methods multiple times instead of that you can do
int lowIndex = 0;
highIndex = highScore(teamScores, highIndex);
lowIndex = lowScore(teamScores, lowIndex);
System.out.println();
System.out.println("The high score is " teamScores[highIndex] " by team " teamNames[highIndex] " and the low score is " teamScores[lowIndex] ".");
Start learning stream
. Its easy and fun ;).
int h1 = IntStream.range(0, teamScores.length)
.reduce((i, j) -> teamScores[i] > teamScores[i] ? i : j)
.getAsInt();
int lowScore = Arrays.stream(teamScores).min().getAsInt();
System.out.println("The high score is " teamScores[h1] " by team " teamNames[h1] " and the low score is " lowScore ".");