Issue
i have an assignment (Java using Netbeans to input Club Name, Input Score, and then Print the table-like league standings. Each line displays the outcome of our input, and show the standings like it should (CLUB Name with biggest point is on top of the table: Here's what i mean: (user input in BOLD)
Input No. of Clubs : 4
Input club 1 : ManUtd
Input club 2 : Liverpool
Input club 3 : Chelsea
Input club 4 : Arsenal
Input Score:
ManUtd x Liverpool : 1 1
ManUtd x Chelsea : 1 0
ManUtd x Arsenal: 1 0
Liverpool x Chelsea : 1 1
Liverpool x Arsenal : 1 0
Chelsea x Arsenal : 1 1
and displays the output of team records like this :
Team Played Win Draw Lose Pts
Manutd 3 2 1 0 7
Liverpool 3 1 2 0 5
Chelsea 3 0 2 1 2
Arsenal 3 0 1 2 1
++++++++++++++++++++++++++++++++++++++++++++++
I have tried to connect the whole knowledge that I have ever read from the textbook but it's not working good. But don't know how to use the array, looping function, integer/data from that input .I cannot really realise the hint that can guide me to the solution. Anyone can help me please? I have just learned java by myself and COVID-19 makes studies difficult to meet up to my lecturer.
For anyone who reply this, thanks for the help :)
my basic code stuck in here:
package standings;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LeagueStandings {
public static void main(String[] args) {
BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
int data = 0;
String[] nama = new String[30];
System.out.println("CLUB NAME");
System.out.println("+===============INPUTAN============================+");
try{
System.out.println("How Many Clubs ? = ");
data = Integer.parseInt(input.readLine());
for (int a=1;a<=data;a++){
System.out.println("------Club No."+ a +"------");
System.out.println("Enter club name = ");
nama[a] = input.readLine();
}
} catch (IOException e ){
System.out.println("Error");
}
}
The result expected like this (result expected from the lecturer)
Solution
Here is the solution to your problem. I tried to write as naive solution as possible as you are a beginner.
package com.standings;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LeagueStandings {
public static void main(String[] args) {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
int data = 0;
System.out.print("Input number of clubs:\t");
data = Integer.parseInt(input.readLine());
String[] teamList = new String[data];
for (int i = 0; i < data; i++) {
System.out.print("Enter club " + (i + 1) + " :\t");
teamList[i] = input.readLine();
}
System.out.println("-------------------------------------------------");
// representing 2D array of team score board where each scoreBoard is of type
// 0 1 2 3 4
// [played] [win] [draw] [loose] [points]
int[][] scoreBoard = new int[data][5];
for (int i = 0; i < data - 1; i++) {
for (int j = i + 1; j < data; j++) {
System.out.print(teamList[i] + " X " + teamList[j] + " : ");
String score = input.readLine();
List<String> scoreList = new ArrayList<String>(Arrays.asList(score.split(" ")));
scoreList.removeIf(s -> s.equals(""));
int teamAScore = Integer.parseInt(scoreList.get(0));
int teamBScore = Integer.parseInt(scoreList.get(1));
scoreBoard[i][0]++;
scoreBoard[j][0]++;
if (teamAScore > teamBScore) {
scoreBoard[i][1]++;
scoreBoard[j][3]++;
scoreBoard[i][4] += 3;
} else if (teamAScore == teamBScore) {
scoreBoard[i][2]++;
scoreBoard[j][2]++;
scoreBoard[i][4]++;
scoreBoard[j][4]++;
} else {
scoreBoard[i][3]++;
scoreBoard[j][1]++;
scoreBoard[j][4] += 3;
}
}
}
System.out.format("%15s%15s%15s%15s%15s%15s\n", "TEAMS", "PLAYED", "WIN", "DRAW", "LOOSE", "POINTS");
for (int i = 0; i < data; i++) {
System.out.format("%15s", teamList[i]);
System.out.format("%15d%15d%15d%15d%15d\n", scoreBoard[i][0], scoreBoard[i][1], scoreBoard[i][2], scoreBoard[i][3], scoreBoard[i][4]);
}
} catch (IOException e) {
System.out.println("Error");
}
}
}
Answered By - CodeTalker