FootballTeamNames

Ques 9(Page 125)
mail@pastecode.io avatar
unknown
java
a year ago
1.5 kB
1
Indexable
Never
import java.util.Scanner;

public class FootballTeamNames {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter number of teams: ");
        int numberOfTeams = scanner.nextInt();
        scanner.nextLine();

        String[] teamNames = new String[numberOfTeams];
        System.out.println("Enter team names:");

        for (int i = 0; i < numberOfTeams; i++) {
            System.out.print("Team " + (i + 1) + ": ");
            teamNames[i] = scanner.nextLine();
        }

        displayTeamNames(teamNames);
    }

    public static void displayTeamNames(String[] teamNames) {
        int maxLength = 0;
        for (String name : teamNames) {
            if (name.length() > maxLength) {
                maxLength = name.length();
            }
        }

        char[][] nameGrid = new char[maxLength][teamNames.length];

        for (int i = 0; i < teamNames.length; i++) {
            String name = teamNames[i];
            for (int j = 0; j < maxLength; j++) {
                if (j < name.length()) {
                    nameGrid[j][i] = name.charAt(j);
                } else {
                    nameGrid[j][i] = ' ';
                }
            }
        }

        for (int i = 0; i < maxLength; i++) {
            for (int j = 0; j < teamNames.length; j++) {
                System.out.print(nameGrid[i][j] + "  ");
            }
            System.out.println();
        }
    }
}