Home > other >  Combining array lists to print results
Combining array lists to print results

Time:12-07

Trying to combine elements from two different arrays (in this problem, attaching the right soccer player with their jersey number). So, result should look like:

Enter jersey number: 2
You have entered jersey number 2, player is "insert player"

Also, something when a number not in the array is listed "Jersey number not enlisted"

import java.util.*;

class ArrayExample
{
    public static void main(String[] args)
    {
        //represent the first 5 football players for USA
        [1] GK  Matt Turner
        [2] DF  Sergiño Dest    
        [3] DF  Walker Zimmerman
        [4] MF  Tyler Adams
        [5] DF  Antonee Robinson
    
        //int [] arr = {1, 2, 3, 4, 5}; 
        String[] player = {"Matt Turner", "Sergiño Dest", "Walker Zimmerman", "Tyler Adams", "Antonee Robinson"};
         
        
    
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter jersey number");
        int i = sc.nextInt();
        System.out.println("You have entered jersey number "   i);
        
        //System.out.println(arr[0]);
        System.out.println(player[i-1]);
        
        
        
    }
}

CodePudding user response:

String[] player = {"Matt Turner", "Sergiño Dest", "Walker Zimmerman", "Tyler Adams", "Antonee Robinson"};

Scanner in = new Scanner(System.in);
System.out.println("Enter jersey number");

int i = in.nextInt();


System.out.println("You have entered jersey number "   i);

if( i < 1 || i > player.length) {
    System.out.println("Jersey number not enlisted");
} else {
    System.out.println(player[i-1]);
}

CodePudding user response:

import java.util.*;
class ArrayExample
{
public static void main(String[] args)
{
    //represent the first 5 football players for USA
    //[1] GK  Matt Turner
    //[2] DF  Sergiño Dest    
    //[3] DF  Walker Zimmerman
    //[4] MF  Tyler Adams
    //[5] DF  Antonee Robinson
    int arr [] = {1, 2, 3, 4, 5}; 
    String player[] = {"Matt Turner", "Sergiño Dest", "Walker Zimmerman", 
    "Tyler Adams", "Antonee Robinson"};
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter jersey number");
    int i = sc.nextInt();
    System.out.println("You have entered jersey number "   i);
    System.out.println(player[i-1]);
    }
}

CodePudding user response:

Your question title is about combining lists but your problem space is much better represented as a map from jersey number to player than as an array or list:

public static void main(String[] args)
{
    Map<Integer,String> players = Map.of(
    1, "Matt Turner",
    2, "Sergiño Dest",...);
    ...
    System.out.println("You have entered jersey number "   i);
    if (players.containsKey(i)) {
        System.out.println(players.get(i));   
    } else {
        System.out.println("There is no player with that jersey");   
    }
}

In most cases overloading the index of an array with meaning such as identifier is a mistake. In your case if there's a jersey missing from the team you'll end up having to represent it as a null and then all your code will need to code with this possibility.

Given Java is an object oriented language, even better would be to use classes:

enum Position{GK, MF, ...}
record Player(String name, Position position, int jersey) { }
Set<Player> team;
  • Related