Atif Shah
Atif Shah

Reputation: 183

How do you put a string inside a 2d Array

I am trying to write a polling program that takes five issues and puts the issue on the row of a 2d Array. Also how would I make the program count how many times a person rated the issue. For example if five people gave rating of five how would I write the program to count the rating and put it on the 2d Array.

These are the instructions:

Write a simple polling program that:

a) A tabular report with the five topics down the left side and the 10 ratings across the top, listing in each column the number of ratings received for each topic.

b) To the right of each row, show the average of the ratings for that issue.

c) Which issue received the highest point total? Display both the issue and the point total. d) Which issue received the lowest point total? Display both the issue and the point total.

This is my code:

import java.util.Arrays;
import java.util.*;
public class Polling {
/**
 * @param args the command line arguments
 */
 public static String[] issues=new String[20];
public static void main(String[] args) {
  Scanner console=new Scanner(System.in);
   issues[0]="Global Warming";
   issues[1]="Earth Quakes";
   issues[2]="Stopping war";
   issues [3]="Equal Rights";
   issues[4]="Curing Cancer";
   int[][] polling =new int[5][10];
   Random rand=new Random();
   int random=rand.nextInt(9)+5;
   int poll=0;

   String polling2=Arrays.toString(polling);


   for(int i=1;i<random;i++){
       System.out.println("Person"+i);
       System.out.println("Rate these issues from 1-10");

       System.out.println(issues[0]);
       int zero=console.nextInt();
       System.out.println(issues[1]);
       int one=console.nextInt();
       System.out.println(issues[2]);
       int two=console.nextInt();
       System.out.println(issues[3]);
       int three=console.nextInt();
       System.out.println(issues[4]);
       int four=console.nextInt();


}
System.out.println();

Upvotes: 0

Views: 792

Answers (1)

David P&#233;rez Cabrera
David P&#233;rez Cabrera

Reputation: 5048

Something like this?:

public static final String[] ISSUES = {
    "Global Warming",
    "Earth Quakes",
    "Stopping war",
    "Equal Rights",
    "Curing Cancer",};

public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    Random rand = new Random();
    int pollings = rand.nextInt(9) + 5;
    int [][] rates= new int[pollings][ISSUES.length];
    for (int i = 0; i < pollings; i++) {
        System.out.println("Person" + i);
        System.out.println("Rate these issues from 1-10");
        for (int j = 0; j < ISSUES.length; j++) {
            System.out.println(ISSUES[j]);
            rates[i][j] = console.nextInt();
        }
    }


     // ADDED
    int minRating = Integer.MAX_VALUE;
    int maxRating = Integer.MIN_VALUE;
    int minRatingIndex = -1;
    int maxRatingIndex = -1;
    for (int i = 0; i < ISSUES.length; i++) {
        System.out.print(ISSUES[i]+":");
        int rating = 0;
        for (int j = 0; j < pollings; j++) {
            System.out.print("\t"+rates[j][i]);
            rating += rates[j][i];
        }
        double average = ((double)rating)/pollings;
        System.out.println("\tavr: "+average);
        if (rating < minRating ){
            minRating = rating;
            minRatingIndex = i;
        }
        if (rating > maxRating ){
            maxRating = rating;
            maxRatingIndex = i;
        }
    }
    System.out.println("Max points:\t"+ISSUES[maxRatingIndex]+":\t"+maxRating+" points");
    System.out.println("Min points:\t"+ISSUES[minRatingIndex]+":\t"+minRating+" points");

    System.out.println();
}

Upvotes: 1

Related Questions