-1

I'm trying to write a program that prompt the user to enter the number of game players, the game players' names, their scores, and prints their scores' in decreasing order of their scores.

I need to do this using arrays for names and scores. Unfortunately this is all I have.

A sample output

Enter the number of players: n
Enter the name of the player: Ash
Enter the player's score: 1200
Enter the name of the player: Brock
Enter the player's score: 900
Enter the name of the player: Misty
Enter the player's score: 1300
Misty     1300.0
Ash       1200.0
Brock     900.0

import java. util.*;


public class HomeworkAssignment12 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter the number of game players: ");
        int numOfPlayers = input.nextInt();
        String[] names = new String[numOfPlayers];
        double[] scores = new double[numOfPlayers];

        //Trying to store the names the user inputs into the names[] array  
        for ( int i = 0; i < names.length; i++) {
            int index = i;
            System.out.println("Enter a game players name: ");
            names[index] = input.nextLine();
            System.out.println("Enter the player's score: ");
            scores[index] = input.nextDouble();

            //used to check what the loop is doing each iteration
            System.out.println(i);

        }//end for

    }//end main

}//end class
  • 6
    "Unfortunately this is all I have" So... what does this do? What doesn't it do? What stopped you doing that? – Andy Turner Jan 18 '17 at 22:46
  • You should try more. Looks like you are on the right path. Within that for loop you should also ask to enter a player's score and store it into an array. The hard part will be sorting the array and keeping the arrays' indexes together. Good luck! – Sedrick Jan 18 '17 at 22:50
  • Hint: `input.nextDouble()`. – Andreas Dolk Jan 18 '17 at 22:53
  • I guess I'm having trouble storing the name into an index. I just plainly don't know how to do it. Also upon running the code it looks like the loop completely ignores inputting a name and score. It doesn't record input until i = 1 , in other words the loop iterates once before it asks to enter in the name and score. – Alex Casablancas Jan 18 '17 at 23:07
  • After `int numOfPlayers = input.nextInt();` write `input.nextLine();` alone – Frakcool Jan 18 '17 at 23:11

1 Answers1

0

Your nearly there, the following code expands on your great attempt. As for your question of how to add the name into the array using an index? You can simply use the variable i from your for-loop.

(I have left out how to print the values from the two arrays in your desired format as an exercise for you):

import java.util.Scanner;
import java.util.Arrays;

class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter the number of game players: ");
    int numOfPlayers = scanner.nextInt();
    String[] names = new String[numOfPlayers];
    double[] scores = new double[numOfPlayers];

    for (int i=0; i<names.length; i++){
      System.out.print("Enter a players name: ");
      String name = scanner.next();
      names[i] = name;
      System.out.print("Enter " + name  +"\'s score: ");
      while(scanner.hasNext()) {
        if(scanner.hasNextDouble()) {
          double score = scanner.nextDouble();
          scores[i] = score;
          break;
        } else {
          System.out.println("ERROR: Invalid Input");
          System.out.print("Enter " + name  +"\'s score: ");
          scanner.next();
        }
      }
    }
    System.out.println("The names array: " + Arrays.toString(names));
    System.out.println("The scores array: " + Arrays.toString(scores));
  }
}

Exampe Usage:

Enter the number of game players:  2
Enter a players name:  Max
Enter Max's score:  3
Enter a players name:  John
Enter John's score:  6
The names array: [Max, John]
The scores array: [3.0, 6.0]

Try it here!

Sash Sinha
  • 11,515
  • 3
  • 18
  • 35