Click here to Skip to main content
14,732,787 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My third selection search team when i search team 1 Baron but can't display player name and score. The last selection show leader board also not neat. Please get me some idea to solve it

What I have tried:

import java.util.Random;
import java.util.Scanner;
import static array.Main.makeSelection;
import static array.Main.numberInputValidation;
import static array.Main.numberOfTeams;

public class Main {
    static Scanner scanner = new Scanner(System.in);
    static Team[] currentTeams = new Team[10];
    
    public static void main(String[] args){
        createTeams();
        while (true) {
            int selection = menu();
            switch (selection) {
                case 1:
                    addTeam();
                    break;
                case 2:
                    addPlayer();
                    break;
                case 3:
                    searchTeam();
                    break;
                case 4:
                    showLeaderboard();
                    break;
                case 5:
                    System.out.println("\n Thank you for using ");
                    System.exit(1);
            }
        }


    }

    public static int menu() {
        System.out.println(" ");
        System.out.println("1. Add Team              ");
        System.out.println("2. Add player to a team  ");
        System.out.println("3. Search for a team     ");
        System.out.println("4. Show Leaderboard      ");
        System.out.println("5. Exit           ");
        System.out.println("Please select an option : ");

        return makeSelection(5);
    }

    public static void createTeams() {
        String[] teamName = {"Baron", "Eagle", "Ace", "Mango", "Juicy" };
        String[] countryname = {"Malaysia", "Singapore", "Indonesia", "Myanmar", "Philippine" };
        String[] playerNames = {"Ali","Abu","Amad","Chan","Tan","Puri","Aiku","Dina","Kimberly","Niger","Adam","Kok"};
        for (int i = 0; i < teamName.length; i++) {
            currentTeams[i] = new Team(teamName[i], countryname[i]);              
             int randomTeamMemberSize = new Random().nextInt(5 + 1)  + 3;
            for (int j = 0; j < 2 ; j++){
                currentTeams[i].addPlayer(playerNames[new Random().nextInt(11 + 1)], new Random().nextInt(100 + 10));
            }
        }
    }

    public static int makeSelection(int numberOfOptions) {
        int selection = 0;
        while (true) {
            try {
                String select = scanner.nextLine();
                selection = Integer.parseInt(select);
                if (selection <= 0 || selection > numberOfOptions) {
                    throw new NumberFormatException();
                } else {
                    return selection;
                }
            } catch (NumberFormatException e) {
                System.out.println("Invalid option, please enter again : ");
            }
        }
    }

    public static char makeSelection() {
        System.out.println("Do you wish to continue? Y/N");
        String selection = scanner.nextLine();
        while (selection.length() > 1 || (selection.charAt(0) != 'Y' && selection.charAt(0) != 'N')) {
            System.out.println("Invalid option, please enter again: ");
            selection = scanner.nextLine();
        }
        return selection.charAt(0);
    }

    public static int numberInputValidation() {
        int number = 0;
        while (true) {
            try {
                String num = scanner.nextLine();
                number = Integer.parseInt(num);
                if (number < 0) {
                    throw new NumberFormatException();
                }
                return number;
            } catch (NumberFormatException e) {
                System.out.println("Please enter a valid number: ");
            }

        }
    }

    public static int numberOfTeams() {
        int numberOfTeams = 0;
        for (Team currentTeam : currentTeams) {
            if (currentTeam != null) {
                numberOfTeams++;
            } else {
                break;
            }
        }
        return numberOfTeams;
    }

    public static void addTeam() {
        String name, country;
        if (currentTeams[currentTeams.length - 1] == null) {
            System.out.println("Please enter a team name : ");
            name = scanner.nextLine();
            System.out.println("Please enter the team's country:");
            country = scanner.nextLine();
            for (int i = 0; i < currentTeams.length; i++) {
                if (currentTeams[i] == null) {
                    currentTeams[i] = new Team(name, country);
                    break;
                }
            }
            System.out.println("Team added succesfully !");

        } else {
            System.out.println("Sorry, there is currently no slots for a new team");
        }
        System.out.println("Press enter to continue..");
        scanner.nextLine();
    }

    public static void addPlayer() {
        while (true) {
            int numberOfTeams = numberOfTeams();

            System.out.println("No.                  Team                    ");
            for (int i = 0; i < numberOfTeams; i++) {
                System.out.printf("|%-2s.|%-42s|\n", Integer.toString(i + 1), currentTeams[i].getName());
            }
            System.out.println("Please select a team : ");
            int selection = makeSelection(numberOfTeams) - 1;
            if (currentTeams[selection].TeamFull()) {
                System.out.println("Sorry, the team is already full.");
            } else {
                String name;    
                int score;
                System.out.println("Please enter the player's name");
                name = scanner.nextLine();
                System.out.println("Please enter the player's score:");
                score = numberInputValidation();
                currentTeams[selection].addPlayer(name, score);
                System.out.println("" + name + " added to team " + currentTeams[selection].getName() + " successfully !");
            }
            if (makeSelection() == 'N') {
                break;
            }
        }
    }

    public static void searchTeam() {
        while (true) {
            System.out.println("Please enter a team name : ");
            String name = scanner.nextLine();
            Team result = search(name);
            result = search(name);
            if (result == null) {
                System.out.println("No results found !");
            } else {
                System.out.println();
                System.out.printf("Team %s from %s\n", result.getName(), result.getCountryname());
                System.out.println("|No.|       Player's Name        |    Score    |");
                Player[] players = result.getPlayer();
                if (result.TeamEmpty()) {
                    System.out.println("|               Team is Empty!                 |");
                } else {
                    for (int i = 0; i < players.length; i++) {
                        if (players[i] == null) {
                            break;
                        }
                        System.out.printf("|%-2s.|%-28s|%-13s|\n", Integer.toString(i + 1), players[i].getName(), Integer.toString(players[i].getScore()));
                    }
                }
            }
            if (makeSelection() == 'N') {
                break;
            }
        }
    }

    public static Team search(String name) {
        for (Team currentTeam : currentTeams) {
            if (currentTeam == null) {
                break;
            }
            if (currentTeam.getName().equals(name)) {
                return currentTeam;
            }
        }
        return null;
    }

    public static void showLeaderboard() {

        Team[] tmpTeam = new Team[numberOfTeams()];
        System.arraycopy(currentTeams, 0, tmpTeam, 0, tmpTeam.length);

        int n = tmpTeam.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (tmpTeam[j].getScore() < tmpTeam[j + 1].getScore()) {
                    // swap arr[j+1] and arr[j]
                    Team temp = tmpTeam[j];
                    tmpTeam[j] = tmpTeam[j + 1];
                    tmpTeam[j + 1] = temp;
                }
            }
        }
        System.out.println("                   Leaderboard                  ");
        System.out.println("   No.             Team    Country       Score    ");
        for (int i = 0; i < tmpTeam.length; i++) {
            System.out.printf("|%-2s.|%-28s|%-25s|%-13s|", Integer.toString(i + 1), tmpTeam[i].getName(),tmpTeam[i].getCountryname(), Integer.toString(tmpTeam[i].getScore())+"\n");
        } 
        System.out.println("Press enter to continue..");
        scanner.nextLine();

    }


    
}
Posted
Updated 28-Oct-20 23:44pm

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
   
See How do I put my player in my team[^]. since you are both working on the same problem you should get together and share ideas. Or perhaps one of you is a sock puppet?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900