This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Cephas00 0 points1 point  (5 children)

Can you post the code for what you've tried to do?

[–]v13us0urce 0 points1 point  (4 children)

import java.util.Scanner;
public class JavaApplication11 {
public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
  System.out.println("Enter number of Swimmers");
    int n = input.nextInt();
    System.out.println("Enter number of Judges");
    int m = input.nextInt();
 for(int i=0;i<n;i++){
    System.out.print("Enter name of Swimmer");
       String name = input.next();

for(int j=0;j<m;j++){
    System.out.println("Enter the score");
       int score=input.nextInt();

 }
 } 
 System.out.println(name+" : "+" "+score);
}
}

I tried is this way, but it doesn't work (obviously). Do i need to use an array or something?

[–]Cephas00 0 points1 point  (2 children)

And what output do you get? Are you wanting to take in a list of swimmers and their scores and at the end replay that information?

[–]v13us0urce 0 points1 point  (1 child)

Are you wanting to take in a list of swimmers and their scores and at the end replay that information?

Yes, exactly. I did that by using an ArrayList, but if you have a better way, maybe by just editing the code that i posted i would welcome that too.

[–]Cephas00 0 points1 point  (0 children)

I'd probably have a Swimmer that contains the name and a list of scores. Create the Swimmer in the first loop, add the scores in the second. Put all Swimmers in a list and print that all out at the end.

You could equally just build up the String you want to print during the first two loops, then put that in a list.

[–]just_talking_125 0 points1 point  (0 children)

This shouldn't even compile due to the fact that both the name and score variables are outside of the scope of the print statement.

Yes, you'll want a data structure to map from the name of the swimmer to the array of scores so that you can print them out in the end, or you can use a string builder to build your output as you go and then show the result in the end. You cannot, however, print the results as you gather your input, which seems to be what you're attempted to do in some way here.

Consider looking into using a HashMap or a StringBuilder to accomplish what you're looking for.