Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The code ground team is a die-hard fan of Manchester United(#MANU) they want you to create a Java application to complete the following task in the English premier league.

Set up a string variable to hold the following results

String results = "Manchester United 1 Chelsea 0, Arsenal 1 Manchester United 1, Manchester United 3 Fulham 1, Liverpool 1 Manchester United 1, Swansea 2 Manchester United 4";

Write a program to work out how many wins Manchester United had, how many games they drew, and how many Manchester United lost.

Extend the program to work out how many goals Manchester United scored and how many they conceded.

Suppose a win gains you 3 points, a draw 1 point, and a loss no points. Have your program work out how many points in the total Manchester United have acquired.

The output for the above will be

Wins 3

Draws 1

Defeats 1

Goals Scored 10

Goals Conceded 6

Total Points 10

Sample Input:

5

ManchesterUnited 1 Chelsea 0

Arsenal 1 ManchesterUnited 1

ManchesterUnited 3 Fulham 1

Liverpool 2 ManchesterUnited 1

Swansea 2 ManchesterUnited 4

Sample Output:

Wins 3

Draws 1

Defeats 1

Goals Scored 10

Goals Conceded 6

Total Points 10

Hint: In the Input, one of the two teams will always be "ManchesterUnited". The first line of input is the number of games followed by n lines in above format team1 goal team2 goal. The name of a team will be a one-word name.

What I have tried:

import java.util.Scanner;
public class BPL
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        String results="Manchester United 1 Chelsea 0, Arsenal 1 Manchester United 1,Manchester United 3 Fulham 1,Liverpool 1 Manchester United 1,Swansea 2 Manchester United 4";
        String result_array[]=results.split(",");
        int number_of_wins=0;
        int number_of_draws=0;
        int number_of_defeats=0;
        int number_of_points=0;
        int goals_scored=0;
        int goals_conceded=0;
        for(int i=0;i<result_array.length;i++)
        {
           String the_result=getResults(result_array[i].trim());
           String[] match_facts=the_result.split("-");
           if(match_facts[0].equals("0"))
           {
               number_of_defeats++;
           }
           if(match_facts[0].equals("1"))
           {
               number_of_draws++;
               number_of_points=number_of_points + 1;
           }
           if(match_facts[0].equals("3"))
           {
               number_of_wins++;
               number_of_points=number_of_points + 3;
           }
           goals_scored=goals_scored+Integer.parseInt(match_facts[1]);
           goals_conceded=goals_conceded+Integer.parseInt(match_facts[2]);
        }
        System.out.println("Wins "+number_of_wins);
        System.out.println("Draws "+number_of_draws);
        System.out.println("Defeats "+number_of_defeats);
        System.out.println("Goal Scored "+goals_scored);
        System.out.println("Goals Conceded "+goals_conceded);
        System.out.println("Total Points "+number_of_points);
    }
    
    public static String getResults(String result)
    {
        String points_and_goals="";
        String[] result_array=result.split(" ");
        int first_score=0;
        int second_score=0;
        int counter=0;
        for(int i=0;i<result_array.length;i++)
        {
            try
            {
                int outputValue=0;
                outputValue=Integer.parseInt(result_array[i]);
                if(counter==0)
                {
                    first_score=outputValue;
                    counter++;
                }
                else
                {
                    second_score=outputValue;        
                }
            }
            catch(NumberFormatException e){}
        }
        String HOME_WIN="3";
        String AWAY_WIN="3";
        String DRAW="1";
        String LOSS="1";
        int man_utd=result.indexOf("Manchester United");
        if(man_utd==0)
        {
            if(first_score>second_score)
            {
                points_and_goals=HOME_WIN+"-"+first_score+"-"+second_score;    
            }
            else if(second_score>first_score)
            {
                points_and_goals=LOSS+"-"+first_score+"-"+second_score;
            }
            else if(first_score==second_score)
            {
                points_and_goals=DRAW+"-"+first_score+"-"+second_score;
            }
        }
        else if(man_utd>0)
        {
            if(second_score>first_score)
            {
                points_and_goals=AWAY_WIN+"-"+second_score+"-"+first_score;
            }
            else if(first_score>second_score)
            {
                points_and_goals=LOSS+"-"+second_score+"-"+first_score;    
            }
            else if(first_score==second_score)
            {
                points_and_goals=DRAW+"-"+second_score+"-"+first_score;    
            }
        }
        return points_and_goals;
    }
}
Posted
Updated 16-Jan-18 21:09pm
v2
Comments
wseng 17-Jan-18 1:12am    
what's wrong with the current output ?
dude_manni 17-Jan-18 1:16am    
Output
Wins 3
Draws 2
Defeats 0
Goal Scored 10
Goals Conceded 5
Total Points 11

Needed Output

Wins 3
Draws 1
Defeats 1
Goals Scored 10
Goals Conceded 6
Total Points 10

Patrice T 17-Jan-18 2:15am    
show input data.
dude_manni 17-Jan-18 3:00am    
I am not inputting just declared input as result variable
Richard MacCutchan 17-Jan-18 8:37am    
Did you actually check the scores on your input string (String results = "Manchester United 1 Chelsea 0, Arsenal 1 Manchester United 1, Manchester United 3 Fulham 1, Liverpool 1 Manchester United 1, Swansea 2 Manchester United 4";? It shows 3 wins and 2 draws.

1 solution

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:
C#
private 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 your line:
C#
for(int i=0;i<result_array.length;i++)

and run your app. 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?

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!
 
Share this answer
 

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