for my code i have an extra space above the roster input and i dont know how to
ID: 3853664 • Letter: F
Question
for my code i have an extra space above the roster input and i dont know how to get rid of it.
here is the question
This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team.
(1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int array and the ratings in another int array. Output these arrays (i.e., output the roster). (3 pts)
Ex:
(2) Implement a menu of options for a user to modify the roster. Each option is represented by a single character. The program initially outputs the menu, and outputs the menu after a user chooses an option. The program ends when the user chooses the option to Quit. For this step, the other options do nothing. (2 pt)
Ex:
(3) Implement the "Output roster" menu option. (1 pt)
Ex:
(4) Implement the "Update player rating" menu option. Prompt the user for a player's jersey number. Prompt again for a new rating for the player, and then change that player's rating. (1 pt)
Ex:
(5) Implement the "Output players above a rating" menu option. Prompt the user for a rating. Print the jersey number and rating for all players with ratings above the entered value. (2 pts)
Ex:
(6) Implement the "Replace player" menu option. Prompt the user for the jersey number of the player to replace. If the player is in the roster, then prompt again for a new jersey number and rating. Update the replaced player's jersey number and rating. (2 pts)
Ex:
here is my code
import java.util.Scanner;
public class PlayerRoster {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[][] players = new int[5][2];
boolean keepAlive = true;
char input;
for (int i = 0; i < 5; i++) {
System.out.println("Enter player " + (i+1) + "'s jersey number:");
players[i][0] = scan.nextInt();
System.out.println("Enter player " + (i+1) + "'s rating:");
players[i][1] = scan.nextInt();
System.out.println();
}
System.out.println();
outputRoster(players, 0);
while (keepAlive) {
outputMenu();
input = scan.next().charAt(0);
if (input == 'q') {
keepAlive = false;
break;
} else if (input == 'o') {
outputRoster(players, 0);
} else if (input == 'u') {
System.out.println("Enter a jersey number:");
int jerseyNum = scan.nextInt();
System.out.println("Enter a new rating for the player:");
int newRating = scan.nextInt();
for (int l = 0; l < 5; l++) {
if (players[l][0] == jerseyNum) {
players[l][1] = newRating;
}
}
} else if (input == 'a') {
System.out.println("Enter a rating:");
int rating = scan.nextInt();
outputRoster(players, rating);
} else if (input == 'r') {
System.out.println("Enter a jersey number:");
int jerseyNum = scan.nextInt();
boolean exists = true;
for (int l = 0; l < 5; l++) {
if (players[l][0] == jerseyNum) {
System.out.println("Enter a new jersey number:");
players[l][0] = scan.nextInt();
System.out.println("Enter a rating for the new player: ");
players[l][1] = scan.nextInt();
}
}
}
}
return;
}
public static void outputRoster(int[][] players, int min) {
System.out.println(((min>0) ? ("ABOVE " + min) : ("ROSTER")));
int item = 1;
for (int[] player : players) {
if (player[1] > min) {
System.out.println("Player " + item + " -- Jersey number: " + player[0] + ", Rating: " + player[1]);
}
item++;
}
System.out.println();
}
public static void outputMenu() {
System.out.println("MENU");
System.out.println("u - Update player rating");
System.out.println("a - Output players above a rating");
System.out.println("r - Replace player");
System.out.println("o - Output roster");
System.out.println("q - Quit ");
System.out.println("Choose an option:");
}
}
here is what the programs says
Explanation / Answer
All your code is working perfectly. To avoid the extra space, all you have to do is just to tweak with the System.out.println() methods.
The System.out.println() will lead to a newline at the end of the printing the statement. If you don't want that new line (a space above the input), to appear, just use the method System.out.print() That extra ln should be removed.
Here is the revised version for you:
import java.util.Scanner;
public class PlayerRoster {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[][] players = new int[5][2];
boolean keepAlive = true;
char input;
for (int i = 0; i < 5; i++) {
System.out.print("Enter player " + (i+1) + "'s jersey number: ");
players[i][0] = scan.nextInt();
System.out.print("Enter player " + (i+1) + "'s rating: ");
players[i][1] = scan.nextInt();
System.out.println();
}
System.out.println();
outputRoster(players, 0);
while (keepAlive) {
outputMenu();
input = scan.next().charAt(0);
if (input == 'q') {
keepAlive = false;
break;
} else if (input == 'o') {
outputRoster(players, 0);
} else if (input == 'u') {
System.out.print("Enter a jersey number: ");
int jerseyNum = scan.nextInt();
System.out.print("Enter a new rating for the player: ");
int newRating = scan.nextInt();
for (int l = 0; l < 5; l++) {
if (players[l][0] == jerseyNum) {
players[l][1] = newRating;
}
}
} else if (input == 'a') {
System.out.print("Enter a rating: ");
int rating = scan.nextInt();
outputRoster(players, rating);
} else if (input == 'r') {
System.out.print("Enter a jersey number: ");
int jerseyNum = scan.nextInt();
boolean exists = true;
for (int l = 0; l < 5; l++) {
if (players[l][0] == jerseyNum) {
System.out.print("Enter a new jersey number: ");
players[l][0] = scan.nextInt();
System.out.print("Enter a rating for the new player: ");
players[l][1] = scan.nextInt();
}
}
}
}
return;
}
public static void outputRoster(int[][] players, int min) {
System.out.println(((min>0) ? ("ABOVE " + min) : ("ROSTER")));
int item = 1;
for (int[] player : players) {
if (player[1] > min) {
System.out.println("Player " + item + " -- Jersey number: " + player[0] + ", Rating: " + player[1]);
}
item++;
}
System.out.println();
}
public static void outputMenu() {
System.out.println("MENU");
System.out.println("u - Update player rating");
System.out.println("a - Output players above a rating");
System.out.println("r - Replace player");
System.out.println("o - Output roster");
System.out.println("q - Quit ");
System.out.println("Choose an option:");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.