Click here to Skip to main content
15,077,641 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. Newbie here.
I have been given an assignment.
So i want to remove an element from an array according to user input.
How can i do it?

Java
<pre> for (int i = 0; i < teamNo; i++) {
                System.out.println("Enter Name of team " + (i + 1));
                teamName[i] = scan.next();
            }

            for (int a = 0; a < eventNo; a++) {
                for (int i = 0; i < teamNo; i++) {
                    System.out.println("Enter rank of the team " + (teamName[i]) + " on the event " + (a + 1));
                    teamRank = scan.nextInt();
                    int tRank = 0;
                    // scoring system for the teams
                    switch (teamRank) {
                        case 3:
                            tRank = 5;
                            break;
                        case 2:
                            tRank = 10;
                            break;
                        case 1:
                            tRank = 20;
                            break;
                    }
                    if (teamRank == 0 || teamRank >= 4) {
                        System.out.println("The team will not be awarded points");
                    } else {
                        teamScore[i] += tRank;
                        System.out.println(tRank + " points is granted for this event\n");
                    }

                    if (scan.hasNextLine()) {
                        scan.nextLine();
                    }
                }
                int topTScore = 0; // this will be used to find the highest value score
                int topteam = -1; // and this will be the index of the team
                for (int i = 0; i < teamNo; i++) {
                    if (teamScore[i] > topTScore) {
                        topTScore = teamScore[i]; // the highest score (up to now)
                        topteam = i; // save the team number for the one with the highest score
                    }
                }
                System.out.println(teamName[topteam] + " is the winner, with a score of " + topTScore+"\n");
                
                System.out.println("Enter the team names who you wish to remove after event 1");
                
                int removeIndex = 1;
                for (int i = removeIndex; i < teamName.length - 1; i++) {
                    teamName[i] = teamName[i + 1];
                }
            }


This is the code so far.
At the last, i want to remove an element equal to user input.

What I have tried:

this is the one i tried but i still dont know much about this.
Posted
Updated 22-Aug-21 22:15pm
v3

The first thing you need to do is get the team name to remove from the user, then search your teams to find that name - that gives you the index you want.
Then the code you have with small tweaks will remove the team.

Remember to check that the name entered actually exists in the collection before you try to remove it, to reduce the number of teams when it's been deleted, and to "empty" the last element when you have moved it to the one before.
Depending on what your teams collection is, it may be possible to reduce the number of elements - but we have no idea how it is declared so we can't advise on that.
   
Sorry for the bother guys.
I have figured it out myself.

the way was to convert the array into arraylist
and remove the ones in the array that the user want to remove
and then just convert it back to array,
   

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