Im having trouble outputing the question with the most lows scores. (That is a l
ID: 3726748 • Letter: I
Question
Im having trouble outputing the question with the most lows scores. (That is a low score is 2 or lower).
Suppose this URL contains the result of 5 poll questions.
http://45.55.136.114/~dlash/java/scores.txt
Respondents rated questions from 1-5 where
1 -> meant they dis-liked the item
4 -> meant they like the item
Create a program that:
1.Reads this URL data,
2.counts the number of responses
3. calculates the average response per question
4. and shows the question with the most lows scores. (That is a low score is 2 or lower)
For example, your output may look like the following:
Q1 Q2 Q3 Q4 Q5
3.5 2.3 3.4 2.6 3.2
The total number of responses: 14
The question with the most ratings 2 or lower: Q2
---------------------------------------------------------------
Heres my program so far in java:
import java.io.IOException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Question1 {
public static void main(String[] args) {
String URLString = "http://45.55.136.114/~dlash/java/scores.txt";
try {
java.net.URL url = new java.net.URL(URLString);
int count = 0;
Scanner input = new Scanner(url.openStream());
input.nextLine();
double q1 = 0;
double q2 = 0;
double q3 = 0;
double q4 = 0;
double q5 = 0;
while (input.hasNext()) {
String line = input.nextLine();
String toks[] = line.split(",");
double a = 0;
double b = 0;
double c = 0;
double d = 0;
double e = 0;
for (int i = 0; i < toks.length; i++) {
a = Double.parseDouble(toks[0]);
b = Double.parseDouble(toks[1]);
c = Double.parseDouble(toks[2]);
d = Double.parseDouble(toks[3]);
e = Double.parseDouble(toks[4]);
}
q1 += a;
q2 += b;
q3 += c;
q4 += d;
q5 += e;
count += toks.length;
}
int numQ = count / 5;
DecimalFormat numberFormat = new DecimalFormat("#.00");
System.out.println("Q1 Q2 Q3 Q4 Q5");
System.out.println(q1 + " " + q2 + " " + q3 + " " + q4 + " " + q5);
System.out.println("Q1Average Q2Average Q3Average Q4Average Q5Average");
System.out.print(numberFormat.format(q1 / numQ) + " " + numberFormat.format(q2 / numQ) + " "
+ numberFormat.format(q3 / numQ) + " " + numberFormat.format(q4 / numQ) + " "
+ numberFormat.format(q5 / numQ));
System.out.println(" The total number of responses: " + numQ);
System.out.println("The question with the most ratings 2 or lower: " + URLString);
} catch (java.net.MalformedURLException ex) {
System.out.println("Invalid URL");
} catch (java.io.IOException ex) {
System.out.println("IO Errors");
}
}
}
Explanation / Answer
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {
String URLString = "http://45.55.136.114/~dlash/java/scores.txt";
try {
java.net.URL url = new java.net.URL(URLString);
int count = 0;
Scanner input = new Scanner(url.openStream());
input.nextLine();
double q1 = 0;
double q2 = 0;
double q3 = 0;
double q4 = 0;
double q5 = 0;
while (input.hasNext()) {
String line = input.nextLine();
String toks[] = line.split(",");
double a = 0;
double b = 0;
double c = 0;
double d = 0;
double e = 0;
a = Double.parseDouble(toks[0]);
b = Double.parseDouble(toks[1]);
c = Double.parseDouble(toks[2]);
d = Double.parseDouble(toks[3]);
e = Double.parseDouble(toks[4]);
q1 += a;
q2 += b;
q3 += c;
q4 += d;
q5 += e;
count += toks.length;
}
int numQ = count / 5;
DecimalFormat numberFormat = new DecimalFormat("#.00");
System.out.println("Q1 Q2 Q3 Q4 Q5");
System.out.println(q1 + " " + q2 + " " + q3 + " " + q4 + " " + q5);
System.out.println("Q1Average Q2Average Q3Average Q4Average Q5Average");
System.out.print(numberFormat.format(q1 / numQ) + " " + numberFormat.format(q2 / numQ) + " "
+ numberFormat.format(q3 / numQ) + " " + numberFormat.format(q4 / numQ) + " "
+ numberFormat.format(q5 / numQ));
System.out.println(" The total number of responses: " + numQ);
//sort average ratings and print the Question with that average
double array[] =new double[]{q1,q2,q3,q4,q5};
//sort
Arrays.sort(array);
//Get Element at 0 index
double average=array[0];
//based on this average find question
String question="";
if(average==q1)
{
question+="Q1";
}
if(average==q2)
{
question+="Q2";
}
if(average==q3)
{
question+="Q3";
}
if(average==q4)
{
question+="Q4";
}
if(average==q5)
{
question+="Q5";
}
System.out.println("The question with the most ratings 2 or lower: " + question);
} catch (java.net.MalformedURLException ex) {
System.out.println("Invalid URL");
} catch (java.io.IOException ex) {
System.out.println("IO Errors");
}
}
}
output
Q1 Q2 Q3 Q4 Q5
21.0 28.0 33.0 30.0 25.5
Q1Average Q2Average Q3Average Q4Average Q5Average
1.50 2.00 2.36 2.14 1.82
The total number of responses: 14
The question with the most ratings 2 or lower: Q1